Эх сурвалжийг харах

ssh proxy. not working missing syncronization

Douglas Andreani 5 жил өмнө
parent
commit
bd7e1a6675
4 өөрчлөгдсөн 160 нэмэгдсэн , 26 устгасан
  1. 17 0
      .vscode/launch.json
  2. 56 26
      main.go
  3. 82 0
      proxy.go
  4. 5 0
      todo.md

+ 17 - 0
.vscode/launch.json

@@ -0,0 +1,17 @@
+{
+    // Use IntelliSense to learn about possible attributes.
+    // Hover to view descriptions of existing attributes.
+    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "name": "Launch",
+            "type": "go",
+            "request": "launch",
+            "mode": "auto",
+            "program": "${fileDirname}",
+            "env": {},
+            "args": []
+        }
+    ]
+}

+ 56 - 26
main.go

@@ -1,13 +1,12 @@
 package main
 
 import (
-	"bufio"
 	"fmt"
-	"io/ioutil"
+	"io"
 	"net"
 	"os"
 	"strconv"
-	"strings"
+	"time"
 )
 
 // TCP4Server will open listeners and create Go Handlers
@@ -26,43 +25,74 @@ func TCP4Server(port int) {
 
 }
 
-// ServerHandler will receive data from the socket and answer to it
-func ServerHandler(c net.Conn) {
+func read(c net.Conn, ch chan []byte) {
+	data := make([]byte, 0, 16384)
+	tmp := make([]byte, 256)
 
 	for {
-
-		r := bufio.NewReader(c)
-		aux, err := r.ReadString('\n')
-		checkError(err)
-		result := strings.TrimSpace(aux)
-		fmt.Println(string(result))
-
-		answer := OpenRequestIPV4(result, 80)
-
-		c.Write([]byte(answer))
+		size, err := c.Read(tmp)
+
+		if err != nil {
+			if err == io.EOF {
+				break
+			}
+			fmt.Printf("\n\n\n### Error on read %v", err)
+			os.Exit(1)
+		}
+		data = append(data, tmp[:size]...)
+		if size < 256 {
+			ch <- data
+		}
 	}
+}
 
-	c.Close()
+// ServerHandler will receive data from the socket and answer to it
+func ServerHandler(c net.Conn) {
 
-}
+	p := NewProxy()
+	ip := "192.168.100.4"
+	p.SetAddrAndPort(ip, 22)
+	p.OpenConnection()
+	p.CheckError()
 
-// OpenRequestIPV4 Opens a request to an ipv4 tcp network address
-func OpenRequestIPV4(ipv4Address string, port int) string {
+	chServer := make(chan []byte, 10)
+	chClient := make(chan []byte, 10)
 
-	service := ipv4Address + ":" + strconv.Itoa(port)
-	tcpv4Addr, _ := net.ResolveTCPAddr("tcp", service)
-	conn, _ := net.DialTCP("tcp4", nil, tcpv4Addr)
-	_, _ = conn.Write([]byte("HEAD / HTTP/1.0\r\n\r\nGet index.html"))
+	sServer := make(chan []byte, 10)
+	sClient := make(chan []byte, 10)
 
-	result, _ := ioutil.ReadAll(conn)
+	for {
+		go p.ReadAll(chServer)
+		go read(c, chClient)
+
+		select {
+		case d := <-chClient:
+			sServer <- d
+			p.WriteBytes(sServer)
+			fmt.Printf("\n\n-->Client: %s", string(d))
+		case d := <-chServer:
+			sClient <- d
+			write(c, sClient)
+			fmt.Printf("\n\n-->SSH Server: %s", string(d))
+		default:
+			fmt.Println("server...")
+
+		}
+		time.Sleep(30 * time.Millisecond)
+	}
+}
 
-	return string(result)
+func write(c net.Conn, ch chan []byte) {
+	select {
+	case d := <-ch:
+		c.Write(d)
+	}
 
 }
 
 func checkError(err error) {
 	if err != nil {
-		fmt.Println(err.Error())
+		fmt.Printf("\n\n\n### Error: %v", err)
 		os.Exit(1)
 	}
 }

+ 82 - 0
proxy.go

@@ -0,0 +1,82 @@
+package main
+
+import (
+	"fmt"
+	"io"
+	"net"
+	"os"
+)
+
+// Proxy struct to hold connection data
+type Proxy struct {
+	Conn *net.TCPConn
+	Addr *net.TCPAddr
+	err  error
+}
+
+func (p *Proxy) SetAddrAndPort(ip string, port int) {
+	address := fmt.Sprintf("%s:%d", ip, port)
+	p.Addr, p.err = net.ResolveTCPAddr("tcp4", address)
+	p.CheckError()
+
+}
+
+// OpenConnection opens dials the tcp connection
+func (p *Proxy) OpenConnection() {
+	p.Conn, p.err = net.DialTCP("tcp4", nil, p.Addr)
+	p.CheckError()
+
+}
+
+func (p *Proxy) WriteBytes(ch chan []byte) {
+
+	select {
+	case d := <-ch:
+		p.Conn.Write(d)
+	}
+	p.CheckError()
+
+}
+
+func (p *Proxy) ReadAll(ch chan []byte) {
+
+	data := make([]byte, 0, 16384)
+	tmp := make([]byte, 256)
+	for {
+		size, err := p.Conn.Read(tmp)
+
+		if err != nil {
+			if err == io.EOF {
+				break
+			}
+			fmt.Printf("\n\n\n### Error on read %v", err)
+			os.Exit(1)
+		}
+		data = append(data, tmp[:size]...)
+		if size < 256 {
+			ch <- data
+		}
+	}
+	p.CheckError()
+
+}
+
+func (p *Proxy) CheckError() {
+	if p.err != nil {
+		fmt.Println(p.err)
+		os.Exit(1)
+	}
+}
+
+func NewProxy() *Proxy {
+	return &Proxy{}
+}
+
+func ReplaceNonPrintable(data []byte) []byte {
+	for i, b := range data {
+		if (b < 32 && b != 13 && b != 10) || b > 127 {
+			data[i] = 0
+		}
+	}
+	return data
+}

+ 5 - 0
todo.md

@@ -0,0 +1,5 @@
+# Proxd TODO List
+
+* [ ] Estabilish a ssh proxy
+* [ ] Generalize ssh proxy to other tcp servers (ftp, mail, mysql...)
+* [ ] Create interface to generate dinamic proxies