-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
162 lines (147 loc) · 4.09 KB
/
app.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
package main
import (
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
)
const ContentLength = 1024 * 64 * 10
type App struct {
Logger
bindAddress string
certFile string
keyFile string
timeout time.Duration
endpoints Endpoints
noTarpit bool
mu sync.Mutex
}
func (app *App) Run() error {
for url, ep := range app.endpoints {
http.HandleFunc(fmt.Sprintf("GET %s", url), func(w http.ResponseWriter, r *http.Request) {
app.mu.Lock()
defer app.mu.Unlock()
requestIP := strings.SplitN(r.RemoteAddr, ":", 2)[0]
ln, err := app.StartListener(requestIP, ep)
if err != nil {
app.PrintError(err.Error())
}
addr, _ := ln.Addr().(*net.TCPAddr)
port := fmt.Sprintf("%d", addr.Port)
if _, err = w.Write([]byte(port)); err != nil {
app.Error(err.Error())
}
})
}
http.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
content := []byte("User-agent: *\nDisallow: /\n")
if _, err := w.Write(content); err != nil {
app.Error(err.Error())
}
})
if !app.noTarpit {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", fmt.Sprintf("%d", ContentLength))
w.WriteHeader(http.StatusNotFound)
payload := []byte("💔")
for {
time.Sleep(time.Second)
if _, err := fmt.Fprint(w, payload); err != nil {
app.Debug(err.Error())
return
}
w.(http.Flusher).Flush()
}
})
}
logRequest := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
addr, _ := net.ResolveTCPAddr("tcp", r.RemoteAddr)
start := time.Now()
defer func() {
lifetime := int(time.Now().Sub(start).Seconds())
app.Info("http/closed", "remoteAddr", addr, "url", r.URL.String(), "lifetime", lifetime)
}()
app.Info("http/connected",
"remoteAddr", addr,
"agent", r.UserAgent(),
"method", r.Method,
"url", r.URL.String())
http.DefaultServeMux.ServeHTTP(w, r)
})
addr, _ := net.ResolveTCPAddr("tcp", app.bindAddress)
if app.certFile != "" && app.keyFile != "" {
if addr.Port == 80 {
addr.Port = 443
}
app.Info("http/listening", "addr", addr)
return http.ListenAndServeTLS(addr.String(), app.certFile, app.keyFile, logRequest)
} else {
app.Info("Cert and key files are not provided: TLS is disabled...")
app.Info("http/listening", "addr", addr)
return http.ListenAndServe(addr.String(), logRequest)
}
}
func (app *App) StartListener(clientIP, endpoint string) (net.Listener, error) {
ln, err := net.Listen("tcp", ":0")
if err != nil {
return nil, err
}
app.Info("tcp/listening", "addr", ln.Addr())
go app.Accept(clientIP, ln, endpoint)
return ln, nil
}
func (app *App) Accept(requestIP string, listener net.Listener, endpoint string) {
ln, _ := listener.(*net.TCPListener)
defer func() {
_ = ln.Close()
app.Info("tcp/closed", "addr", ln.Addr())
}()
if err := ln.SetDeadline(time.Now().Add(app.timeout)); err != nil {
app.Error(err.Error())
return
}
for {
remoteConn, err := ln.Accept()
if err != nil {
if !errors.Is(err, os.ErrDeadlineExceeded) {
app.Error(err.Error())
}
return
}
app.Info("tcp/connected", "laddr", remoteConn.LocalAddr(), "raddr", remoteConn.RemoteAddr())
addr, _ := remoteConn.RemoteAddr().(*net.TCPAddr)
remoteIP := fmt.Sprintf("%s", addr.IP)
if requestIP != remoteIP {
_ = remoteConn.Close()
app.Error("ip_mismatch", "requestIP", requestIP, "clientIP", remoteIP)
continue
}
go app.Connect(remoteConn, endpoint)
return
}
}
func (app *App) Connect(remoteConn net.Conn, localAddr string) {
defer func() {
_ = remoteConn.Close()
app.Debug("tcp/closed", "laddr", remoteConn.LocalAddr(), "raddr", remoteConn.RemoteAddr())
}()
localConn, err := net.Dial("tcp", localAddr)
if err != nil {
app.Error(err.Error())
return
}
defer func() {
_ = localConn.Close()
app.Debug("tcp/closed", "laddr", localConn.LocalAddr(), "raddr", localConn.RemoteAddr())
}()
app.Info("tcp/connected", "laddr", localConn.LocalAddr(), "raddr", localConn.RemoteAddr())
go io.Copy(remoteConn, localConn)
if _, err = io.Copy(localConn, remoteConn); err != nil {
app.Error(err.Error())
}
}