2020-04-23 08:29:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Data structure to hold specific host details */
|
2020-04-23 19:39:31 +00:00
|
|
|
type ConnHost struct {
|
|
|
|
Name string
|
|
|
|
Port string
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Simple wrapper to Listener that holds onto virtual
|
|
|
|
* host information and generates GophorConn
|
|
|
|
* instances on each accept
|
2020-04-23 08:29:58 +00:00
|
|
|
*/
|
|
|
|
type GophorListener struct {
|
|
|
|
Listener net.Listener
|
2020-04-24 10:21:46 +00:00
|
|
|
Host *ConnHost
|
2020-04-23 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BeginGophorListen(bindAddr, hostname, port string) (*GophorListener, error) {
|
|
|
|
gophorListener := new(GophorListener)
|
2020-04-24 10:21:46 +00:00
|
|
|
gophorListener.Host = &ConnHost{ hostname, port }
|
2020-04-23 08:29:58 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
gophorListener.Listener, err = net.Listen("tcp", bindAddr+":"+port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return gophorListener, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *GophorListener) Accept() (*GophorConn, error) {
|
|
|
|
conn, err := l.Listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gophorConn := new(GophorConn)
|
|
|
|
gophorConn.Conn = conn
|
2020-04-24 10:21:46 +00:00
|
|
|
gophorConn.Host = &ConnHost{ l.Host.Name, l.Host.Port }
|
2020-04-23 08:29:58 +00:00
|
|
|
return gophorConn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *GophorListener) Addr() net.Addr {
|
|
|
|
return l.Listener.Addr()
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Simple wrapper to Conn with easier acccess
|
|
|
|
* to hostname / port information
|
|
|
|
*/
|
|
|
|
type GophorConn struct {
|
|
|
|
Conn net.Conn
|
2020-04-24 10:21:46 +00:00
|
|
|
Host *ConnHost
|
2020-04-23 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GophorConn) Read(b []byte) (int, error) {
|
|
|
|
return c.Conn.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GophorConn) Write(b []byte) (int, error) {
|
|
|
|
return c.Conn.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GophorConn) RemoteAddr() net.Addr {
|
|
|
|
return c.Conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GophorConn) Close() error {
|
|
|
|
return c.Conn.Close()
|
|
|
|
}
|