-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
122 lines (99 loc) · 3.2 KB
/
main.go
File metadata and controls
122 lines (99 loc) · 3.2 KB
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
package main
import (
"aegis/proxy"
"aegis/utils"
"encoding/json"
"errors"
"fmt"
"github.com/alexflint/go-arg"
"net"
"net/http"
)
var Args struct {
Host string `arg:"-h,--host" default:"0.0.0.0"`
ControlPort uint16 `arg:"-c,--control-port" help:"Port to host control http sever" default:"8765"`
Udp bool `arg:"-u,--udp" help:"Use UDP" default:"false"`
HostPort uint16 `arg:"-p,--port,required" help:"HostPort to pkg on"`
Destination string `arg:"-d,--destination,required" help:"Destination of the server proxying to"`
DestinationPort uint16 `arg:"--destination-port" help:"HostPort of the host to pkg to, defaults to provided HostPort"`
}
func receiveAddress(request *http.Request) (string, error) {
var data string
err := json.NewDecoder(request.Body).Decode(&data)
if err != nil {
return "", err
}
if net.ParseIP(data) == nil {
return "", errors.New("input is not a valid ip address")
}
return data, nil
}
func main() {
arg.MustParse(&Args)
if Args.DestinationPort == 0 {
Args.DestinationPort = Args.HostPort
}
banned := NewBanList()
go listenAndProxy(banned)
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
switch request.Method {
case http.MethodGet:
writer.WriteHeader(http.StatusOK)
err := json.NewEncoder(writer).Encode(banned.banMap)
utils.HandleError(err)
break
case http.MethodPost:
ip, err := receiveAddress(request)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
_, err := writer.Write([]byte(fmt.Sprintf("%s", err)))
utils.HandleError(err)
break
}
writer.WriteHeader(http.StatusOK)
banned.AddBan(ip)
fmt.Printf("Added client address %s to the banlist\n", ip)
break
case http.MethodDelete:
ip, err := receiveAddress(request)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
_, err := writer.Write([]byte(fmt.Sprintf("%s", err)))
utils.HandleError(err)
break
}
writer.WriteHeader(http.StatusOK)
banned.RemoveBan(ip)
fmt.Printf("Removed client address %s from the banlist\n", ip)
break
default:
writer.WriteHeader(http.StatusMethodNotAllowed)
_, err := writer.Write([]byte("Unsupported HTTP method"))
utils.HandleError(err)
break
}
})
fmt.Printf("Serving http control api on http://0.0.0.0:%d\n", Args.ControlPort)
err := http.ListenAndServe(fmt.Sprintf(":%d", Args.ControlPort), nil)
utils.HandleError(err)
}
func listenAndProxy(banList *BanList) {
connectionString := fmt.Sprintf("0.0.0.0:%d", Args.HostPort)
fmt.Printf("Listening for connections on tcp://%s to proxy\n", connectionString)
listener, err := net.Listen("tcp4", connectionString)
utils.HandleError(err)
// Close listener when this function returns
defer func(listener net.Listener) {
utils.HandleError(listener.Close())
}(listener)
for {
connection, err := listener.Accept()
utils.HandleError(err)
if banList.IsBanned(connection.RemoteAddr().String()) {
fmt.Printf("Rejecting request from banned client %s\n", connection.RemoteAddr())
continue
}
go proxy.ProxyConnection(connection, fmt.Sprintf("%s:%d", Args.Destination, Args.DestinationPort))
}
}