diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b405c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +*.exe diff --git a/app_test.go b/app_test.go new file mode 100644 index 0000000..edfd5b9 --- /dev/null +++ b/app_test.go @@ -0,0 +1,53 @@ +package sshtunnel + +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 +} + +//function +func TestEndPointParseString(t *testing.T) { + var testRemote Endpoint + var testLocal Endpoint + + // make a string and test what the string should be + testLocal, testRemote = ParseEndpointString("5555:motherbrain.unr.edu:5555") + + fmt.Printf("Local: %s %d\n", testLocal.Host, testLocal.Port) + fmt.Printf("Remote: %s %d \n", testRemote.Host, testRemote.Port) + + if testLocal.Host != "localhost"{ + t.Error("Invalid Local EP Hostname") + } + + if testLocal.Port != 5555 { + t.Error("Invalid Local EP Port") + } + + + if testRemote.Host != "motherbrain.unr.edu"{ + t.Error("Invalid Local EP Hostname") + } + + if testRemote.Port != 5555 { + t.Error("Invalid Local EP Port") + } + // return string == stringwant // success +} diff --git a/bin/hostname.go b/bin/hostname.go deleted file mode 100644 index 804d9a4..0000000 --- a/bin/hostname.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import ( - "fmt" - "os" -) - -func main() { - localHostName, err := os.Hostname() - if err != nil{ - panic(err) - } - fmt.Println("Local Host Name:",localHostName) -} diff --git a/bin/localMac_addr.go b/bin/localMac_addr.go deleted file mode 100644 index 31d779f..0000000 --- a/bin/localMac_addr.go +++ /dev/null @@ -1,71 +0,0 @@ -package main - -import ( - "fmt" - "net" - "strings" - "os" - "log" -) - -func main() { - // localMacAddr, err := net.() - // if err != nil{ - // panic(err) - // } - // fmt.Println("Local MAC addr:",localMacAddr) - // - // - - var currentIP, currentNetworkHardwareName string - - currentIP = "134.197.41.183/22" - - interfaces, _ := net.Interfaces() - for _, interf := range interfaces { - if addrs, err := interf.Addrs(); err == nil { - for index, addr := range addrs { - if addr.String() != "127.0.0.1/8" { - log.Println("[", index, "]", interf.Name, ">", addr) - - // only interested in the name with current IP address - if strings.Contains(addr.String(), currentIP) { - log.Println("Use name : ", interf.Name) - currentNetworkHardwareName = interf.Name - } - } - } - } - } - - log.Println("-------------------------------------------") - - // extract the hardware information base on the interface name - // capture above - netInterface, err := net.InterfaceByName(currentNetworkHardwareName) - - if err != nil { - fmt.Println(err) - } - - name := netInterface.Name - macAddress := netInterface.HardwareAddr - - log.Println("Hardware name : ", name) - log.Println("MAC address : ", macAddress) - - // verify if the MAC address can be parsed properly - hwAddr, err := net.ParseMAC(macAddress.String()) - if err != nil { - log.Println("No able to parse MAC address : ", err) - os.Exit(-1) - } - log.Println("Physical hardware address : %s \n", hwAddr.String()) - -} - - - -// net.Interfaces() // returns an array of interfaces -// net.InterfaceByName(oneOfTheReturnedInterfaces) -// ^.HardwareAddr diff --git a/bin/main.go b/bin/main.go index 3579cba..4de2ca6 100644 --- a/bin/main.go +++ b/bin/main.go @@ -1,12 +1,15 @@ package main import ( "sshtunnel" + "fmt" ) func main() { + var ep = &sshtunnel.Endpoint{}; var sshClient = &sshtunnel.Client{}; sshClient.Start() + fmt.Printf("%v", ep) } diff --git a/bin/main.manifest b/bin/main.manifest deleted file mode 100644 index e69de29..0000000 diff --git a/endpoint/endpoint.go b/endpoint/endpoint.go index 3287e6a..78cc77b 100644 --- a/endpoint/endpoint.go +++ b/endpoint/endpoint.go @@ -1,86 +1,85 @@ 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 index 296aeed..d398c85 100644 --- a/endpoint/endpoint_test.go +++ b/endpoint/endpoint_test.go @@ -1,23 +1,37 @@ package endpoint import ( "testing" "fmt" + "sshtunnel" ) //function func TestEndPointString(t *testing.T) { - var testend Endpoint + var testend sshtunnel.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 } + +//function +func TestEndPointParseString(t *testing.T) { + var testRemote sshtunnel.Endpoint + var testLocal sshtunnel.Endpoint + + // make a string and test what the string should be + testLocal, testRemote = sshtunnel.ParseEndpointString("5555:motherbrain.unr.edu:5555") + + fmt.Printf("Local: %s %s\n", testLocal.Host, testLocal.Port) + fmt.Printf("Remote: %s %s \n", testRemote.Host, testRemote.Port) + // return string == stringwant // success +}