diff --git a/endpoint/endpoint.go b/endpoint/endpoint.go new file mode 100644 index 0000000..3287e6a --- /dev/null +++ b/endpoint/endpoint.go @@ -0,0 +1,86 @@ +package endpoint + +import ( + + "fmt" + "io" + + "golang.org/x/crypto/ssh" + + + "net" +) + +type Endpoint struct { + Host string + Port int +} + + +func (endpoint *Endpoint) String() string { + return fmt.Sprintf("%s:%d", endpoint.Host, endpoint.Port) +} + +type SSHtunnel struct { + Local *Endpoint + Server *Endpoint + Remote *Endpoint + + Config *ssh.ClientConfig + ServerConnection *ssh.ServerConfig +} + +// type localEndpoint []Endpoint +// +// type remoteEndpoint []Endpoint + + +//func (index *arrayFlags) Set + +// var testend Endpoint +// +// testend.Host = "localhost" +// testend.Port = "5" + +func (tunnel *SSHtunnel) Start() error { + for { + listener, err := net.Listen("tcp", tunnel.Local.String()) + if err != nil { + return err + } + defer listener.Close() + + for { + conn, err := listener.Accept() + if err != nil { + return err + } + go tunnel.forward(conn) + } +} + } + + +func (tunnel *SSHtunnel) forward(localConn net.Conn) { + serverConn, err := ssh.Dial("tcp", tunnel.Server.String(), tunnel.Config) + if err != nil { + fmt.Printf("Server dial error: %s\n", err) + return + } + + remoteConn, err := serverConn.Dial("tcp", tunnel.Remote.String()) + if err != nil { + fmt.Printf("Remote dial error: %s\n", err) + return + } + + copyConn:=func(writer, reader net.Conn) { + _, err:= io.Copy(writer, reader) + if err != nil { + fmt.Printf("io.Copy error: %s", err) + } + } + + go copyConn(localConn, remoteConn) + go copyConn(remoteConn, localConn) +} diff --git a/endpoint/endpoint_test.go b/endpoint/endpoint_test.go new file mode 100644 index 0000000..296aeed --- /dev/null +++ b/endpoint/endpoint_test.go @@ -0,0 +1,23 @@ +package endpoint + +import ( + "testing" + "fmt" +) + +//function +func TestEndPointString(t *testing.T) { + var testend Endpoint + + testend.Host = "localhost" + testend.Port = 5555 + + // make a string and test what the string should be + stringWant := "localhost:5555" + + returnedString := testend.String() + + fmt.Printf("stringWant: %s\n", stringWant) + fmt.Printf("returnedString : %s\n", returnedString) + // return string == stringwant // success +}