-
Notifications
You must be signed in to change notification settings - Fork 23
/
rserver.go
177 lines (162 loc) · 4.88 KB
/
rserver.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"crypto/tls"
"fmt"
"io"
"log"
"net"
"os"
"bufio"
"github.com/hashicorp/yamux"
"github.com/kost/tty2web/tlshelp"
"strconv"
"strings"
"time"
)
var proxytout = time.Millisecond * 1000 //timeout for wait magicbytes
// listen for agents
func listenForAgents(verbose bool, tlslisten bool, address string, clients string, certificate string, agentpassword string) error {
var err, erry error
var cer tls.Certificate
var session *yamux.Session
var sessions []*yamux.Session
var ln net.Listener
log.Printf("Will start listening for clients on %s", clients)
if tlslisten {
log.Printf("Listening for agents on %s using TLS", address)
if certificate == "" {
cer, err = tlshelp.GetRandomTLS(2048)
log.Println("No TLS certificate. Generated random one.")
} else {
cer, err = tls.LoadX509KeyPair(certificate+".crt", certificate+".key")
}
if err != nil {
log.Println(err)
return err
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
ln, err = tls.Listen("tcp", address, config)
} else {
log.Printf("Listening for agents on %s", address)
ln, err = net.Listen("tcp", address)
}
if err != nil {
log.Printf("Error listening on %s: %v", address, err)
return err
}
var listenstr = strings.Split(clients, ":")
portnum, errc := strconv.Atoi(listenstr[1])
if errc != nil {
log.Printf("Error converting listen str %s: %v", clients, errc)
}
portinc := 0
for {
conn, err := ln.Accept()
conn.RemoteAddr()
agentstr := conn.RemoteAddr().String()
log.Printf("[%s] Got a connection from %v: ", agentstr, conn.RemoteAddr())
if err != nil {
fmt.Fprintf(os.Stderr, "Errors accepting!")
}
reader := bufio.NewReader(conn)
//read only 64 bytes with timeout=1-3 sec. So we haven't delay with browsers
conn.SetReadDeadline(time.Now().Add(proxytout))
statusb := make([]byte, 64)
_, _ = io.ReadFull(reader, statusb)
//Alternatively - read all bytes with timeout=1-3 sec. So we have delay with browsers, but get all GET request
//conn.SetReadDeadline(time.Now().Add(proxytout))
//statusb,_ := ioutil.ReadAll(magicBuf)
//log.Printf("magic bytes: %v",statusb[:6])
//if hex.EncodeToString(statusb) != magicbytes {
if string(statusb)[:len(agentpassword)] != agentpassword {
//do HTTP checks
log.Printf("Received request: %v", string(statusb[:64]))
status := string(statusb)
if strings.Contains(status, " HTTP/1.1") {
httpresonse := "HTTP/1.1 301 Moved Permanently" +
"\r\nContent-Type: text/html; charset=UTF-8" +
"\r\nLocation: https://www.microsoft.com/" +
"\r\nServer: Apache" +
"\r\nContent-Length: 0" +
"\r\nConnection: close" +
"\r\n\r\n"
conn.Write([]byte(httpresonse))
conn.Close()
} else {
conn.Close()
}
} else {
//magic bytes received.
//disable socket read timeouts
log.Printf("[%s] Got Client from %s", agentstr, conn.RemoteAddr())
conn.SetReadDeadline(time.Now().Add(100 * time.Hour))
//Add connection to yamux
session, erry = yamux.Client(conn, nil)
if erry != nil {
log.Printf("[%s] Error creating client in yamux for %s: %v", agentstr, conn.RemoteAddr(), erry)
continue
}
sessions = append(sessions, session)
go listenForClients(verbose, agentstr, listenstr[0], portnum+portinc, session)
portinc = portinc + 1
}
}
return nil
}
// Catches local clients and connects to yamux
func listenForClients(verbose bool, agentstr string, listen string, port int, session *yamux.Session) error {
var ln net.Listener
var address string
var err error
portinc := port
for {
address = fmt.Sprintf("%s:%d", listen, portinc)
log.Printf("[%s] Waiting for clients on %s", agentstr, address)
ln, err = net.Listen("tcp", address)
if err != nil {
log.Printf("[%s] Error listening on %s: %v", agentstr, address, err)
portinc = portinc + 1
} else {
break
}
}
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("[%s] Error accepting on %s: %v", agentstr, address, err)
return err
}
if session == nil {
log.Printf("[%s] Session on %s is nil", agentstr, address)
conn.Close()
continue
}
log.Printf("[%s] Got client. Opening stream for %s", agentstr, conn.RemoteAddr())
stream, err := session.Open()
if err != nil {
log.Printf("[%s] Error opening stream for %s: %v", agentstr, conn.RemoteAddr(), err)
return err
}
// connect both of conn and stream
go func() {
if verbose {
log.Printf("[%s] Starting to copy conn to stream for %s", agentstr, conn.RemoteAddr())
}
io.Copy(conn, stream)
conn.Close()
if verbose {
log.Printf("[%s] Done copying conn to stream for %s", agentstr, conn.RemoteAddr())
}
}()
go func() {
if verbose {
log.Printf("[%s] Starting to copy stream to conn for %s", agentstr, conn.RemoteAddr())
}
io.Copy(stream, conn)
stream.Close()
if verbose {
log.Printf("[%s] Done copying stream to conn for %s", agentstr, conn.RemoteAddr())
}
}()
}
}