func connectionDispatcher(listener listener.TrudyListener, name string, show bool) { defer listener.Close() for { fd, conn, err := listener.Accept() if err != nil { continue } var p pipe.TrudyPipe if name == "TLS" { p = new(pipe.TLSPipe) err = p.New(connectionCount, fd, conn) } else { p = new(pipe.TCPPipe) err = p.New(connectionCount, fd, conn) } if err != nil { log.Println("[ERR] Error creating new pipe.") continue } if show { log.Printf("[INFO] ( %v ) %v Connection accepted!\n", connectionCount, name) } go clientHandler(p, show) go serverHandler(p) connectionCount++ } }
func serverHandler(pipe pipe.TrudyPipe) { buffer := make([]byte, 65535) for { bytesRead, err := pipe.ReadDestination(buffer) if err != nil { break } data := module.Data{FromClient: false, Bytes: buffer[:bytesRead], DestAddr: pipe.SourceInfo(), SrcAddr: pipe.DestinationInfo()} if data.Drop() { continue } if data.DoMangle() { data.Mangle() bytesRead = len(data.Bytes) } if data.DoIntercept() { if websocketConn == nil { log.Printf("[ERR] Websocket Connection has not been setup yet! Cannot intercept.") continue } websocketMutex.Lock() bs := fmt.Sprintf("% x", data.Bytes) if err := websocketConn.WriteMessage(websocket.TextMessage, []byte(bs)); err != nil { log.Printf("[ERR] Failed to write to websocket: %v\n", err) websocketMutex.Unlock() continue } _, moddedBytes, err := websocketConn.ReadMessage() websocketMutex.Unlock() if err != nil { log.Printf("[ERR] Failed to read from websocket: %v\n", err) continue } str := string(moddedBytes) str = strings.Replace(str, " ", "", -1) moddedBytes, err = hex.DecodeString(str) if err != nil { log.Printf("[ERR] Failed to decode hexedited data.") continue } data.Bytes = moddedBytes bytesRead = len(moddedBytes) } if data.DoPrint() { log.Printf("%v -> %v\n%v\n", data.DestAddr.String(), data.SrcAddr.String(), data.PrettyPrint()) } _, err = pipe.WriteSource(data.Bytes[:bytesRead]) if err != nil { break } } }