| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- 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)
- block, err := aes.NewCipher(decoded)
- errCheckPanic(err, "Decryption failed %s device #%d", conn.RemoteAddr().String(), id)
- mode := cipher.NewCBCDecrypter(block, payload[:16])
- mode.CryptBlocks(message, payload[16:])
- fmt.Printf("Message: %s\n", string(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)
- }
- }
|