Esempio n. 1
0
func clientHandler(pipe pipe.TrudyPipe, show bool) {
	if show {
		defer log.Printf("[INFO] ( %v ) Closing TCP connection.\n", pipe.Id())
	}
	defer pipe.Close()

	buffer := make([]byte, 65535)

	for {
		bytesRead, err := pipe.ReadSource(buffer)
		if err != nil {
			break
		}
		data := module.Data{FromClient: true,
			Bytes:    buffer[:bytesRead],
			DestAddr: pipe.DestinationInfo(),
			SrcAddr:  pipe.SourceInfo()}
		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.SrcAddr.String(), data.DestAddr.String(), data.PrettyPrint())
		}

		_, err = pipe.WriteDestination(data.Bytes[:bytesRead])
		if err != nil {
			break
		}
	}
}