| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package main
- import (
- "crypto/aes"
- "crypto/cipher"
- "encoding/binary"
- "encoding/hex"
- "fmt"
- "net"
- )
- var aes_key = "03020100070605040b0a09080f0e0d0c"
- func serveConnection(conn net.Conn) {
- defer logPanic()
- defer conn.Close()
- fmt.Printf("Connection from %s\n", conn.RemoteAddr().String())
- decoded, _ := hex.DecodeString(aes_key)
- buf := make([]byte, 4)
- for {
- _, err := conn.Read(buf)
- errCheckPanic(err, "Connection failed with %s", conn.RemoteAddr().String())
- id := binary.LittleEndian.Uint16(buf[:2])
- msglen := binary.LittleEndian.Uint16(buf[2:])
- fmt.Printf("Connection from ID %d message length %d\n %x\n", id, msglen, buf)
- payload := make([]byte, msglen)
- _, err = conn.Read(payload)
- errCheckPanic(err, "Connection failed %s device #%d", conn.RemoteAddr().String(), id)
- fmt.Printf("Payload: %x\n", payload)
- message := make([]byte, msglen-16)
- c, err := aes.NewCipher(decoded)
- errCheckPanic(err, "Decryption failed %s device #%d", conn.RemoteAddr().String(), id)
- cbc := cipher.NewCBCEncrypter(c, payload[:16])
- cbc.CryptBlocks(payload[16:], message)
- fmt.Printf("Message: %x\n", message)
- }
- }
- func serve(address string) {
- ln, err := net.Listen("tcp", address)
- errCheckExit(err,"Failed accept connection")
- defer ln.Close()
- fmt.Printf("Service socket ready at %s\n", address)
- for {
- conn, err := ln.Accept()
- errCheckExit(err,"Failed accept connection %s", conn.RemoteAddr().String())
- go serveConnection(conn)
- }
- }
|