-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.go
executable file
·150 lines (126 loc) · 3.31 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"strconv"
"sync"
"time"
tls "github.com/wwhtrbbtt/utls"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var cert tls.Certificate
var LoadedConfig *Config = &Config{}
var collection *mongo.Collection
var ctx = context.TODO()
var client *mongo.Client
var local = false
var connectedToDB = false
var TCPFingerprints sync.Map
func init() {
// Loads the config and connects to database (if enabled)
err := LoadedConfig.LoadFromFile()
if err != nil {
log.Fatal(err)
}
if len(LoadedConfig.MongoURL) == 0 { // Don't attempt to setup mongo if its not populated in the config
return
}
clientOptions := options.Client().ApplyURI(LoadedConfig.MongoURL)
client, err = mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(LoadedConfig.DB, LoadedConfig.Collection)
collection = client.Database(LoadedConfig.DB).Collection(LoadedConfig.Collection)
connectedToDB = true
}
func redirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, LoadedConfig.HTTPRedirect, http.StatusMovedPermanently)
}
func StartRedirectServer(host, port string) {
// Starts an HTTP server on port 80 that redirects to the HTTPS server on port 443
local = host == "" && port != "443"
log.Println("Starting Redirect Server:", LoadedConfig.HTTPRedirect)
log.Println("Listening on", host+":"+port)
http.HandleFunc("/", redirect)
err := http.ListenAndServe(host+":"+port, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// Timeout function
func timeoutHandleTLSConnection(conn net.Conn) bool {
result := make(chan bool)
go func() {
result <- HandleTLSConnection(conn)
}()
select {
case <-time.After(15 * time.Second):
return false
case tmp := <-result:
return tmp
}
}
func main() {
log.Println("Starting server...")
log.Println("Listening on " + LoadedConfig.Host + ":" + LoadedConfig.TLSPort)
// Load the TLS certificates
var err error
cert, err = tls.LoadX509KeyPair(LoadedConfig.CertFile, LoadedConfig.KeyFile)
if err != nil {
log.Fatal("Error loading TLS certificates", err)
}
// Create a TLS configuration
config := tls.Config{
ServerName: LoadedConfig.Host,
NextProtos: []string{
"h2",
},
Certificates: []tls.Certificate{cert},
}
listener, err := tls.Listen("tcp", LoadedConfig.Host+":"+LoadedConfig.TLSPort, &config)
if err != nil {
log.Fatal("Error starting tcp listener", err)
}
tlsPort, err := strconv.Atoi(LoadedConfig.TLSPort)
if err != nil {
log.Fatal("Error parsing tls port", err)
}
defer listener.Close()
go StartRedirectServer(LoadedConfig.Host, LoadedConfig.HTTPPort)
if LoadedConfig.Device != "" {
go sniffTCP(LoadedConfig.Device, tlsPort)
}
for {
conn, err := listener.Accept()
//fmt.Println(reflect.TypeOf(conn))
if err != nil {
log.Println("Error accepting connection", err)
}
var ip string
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
ip = addr.IP.String()
}
if IsIPBlocked(ip) {
Log("Request from IP " + ip + " blocked")
conn.Write([]byte("Don't waste proxies"))
conn.Close()
} else {
go func() {
success := timeoutHandleTLSConnection(conn)
if !success {
Log("Request aborted - " + ip)
conn.Close()
}
}()
}
}
}