-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
193 lines (168 loc) · 5.33 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"database/sql"
"html/template"
"net"
"net/http"
"os/exec"
"strings"
_ "modernc.org/sqlite"
)
type PortProxy struct {
RuleName string
ListenAddress string
ListenPort string
ConnectAddress string
ConnectPort string
}
var templates = template.Must(template.ParseGlob("templates/*.html"))
var db *sql.DB
func main() {
// Initialize the database
initDB()
http.HandleFunc("/", homeHandler)
http.HandleFunc("/add", addHandler)
http.HandleFunc("/remove", removeHandler)
http.HandleFunc("/list", listHandler)
http.ListenAndServe(":2233", nil)
}
// Initialize SQLite Database
func initDB() {
var err error
// Use "sqlite" instead of "sqlite3" for the driver name
db, err = sql.Open("sqlite", "./rules.db")
if err != nil {
panic(err)
}
// Create table if not exists
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS rules (
id INTEGER PRIMARY KEY AUTOINCREMENT,
rule_name TEXT NOT NULL,
listen_address TEXT NOT NULL,
listen_port TEXT NOT NULL,
connect_address TEXT NOT NULL,
connect_port TEXT NOT NULL
)
`)
if err != nil {
panic(err)
}
}
// Render home page
func homeHandler(w http.ResponseWriter, r *http.Request) {
data := struct {
RouterIP string
}{
RouterIP: getRouterIP(),
}
if err := templates.ExecuteTemplate(w, "home.html", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// Add rule to database + firewall + portproxy
func addHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
ruleName := r.FormValue("rule_name")
listenAddress := r.FormValue("listen_address")
listenPort := r.FormValue("listen_port")
connectAddress := r.FormValue("connect_address")
connectPort := r.FormValue("connect_port")
// Add to database
_, err := db.Exec(`INSERT INTO rules (rule_name, listen_address, listen_port, connect_address, connect_port) VALUES (?, ?, ?, ?, ?)`,
ruleName, listenAddress, listenPort, connectAddress, connectPort)
if err != nil {
http.Error(w, "Failed to save rule to database", http.StatusInternalServerError)
return
}
// Add portproxy rule
addPortProxy := exec.Command("netsh", "interface", "portproxy", "add", "v4tov4",
"listenaddress="+listenAddress, "listenport="+listenPort,
"connectaddress="+connectAddress, "connectport="+connectPort)
if err := addPortProxy.Run(); err != nil {
http.Error(w, "Failed to add port proxy", http.StatusInternalServerError)
return
}
// Add firewall rule
addFirewall := exec.Command("netsh", "advfirewall", "firewall", "add", "rule",
"name="+ruleName, "protocol=TCP", "dir=in",
"localport="+listenPort, "action=allow")
if err := addFirewall.Run(); err != nil {
http.Error(w, "Failed to add firewall rule", http.StatusInternalServerError)
return
}
listHandler(w, r)
}
}
// Remove rule from database + firewall + portproxy
func removeHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
ruleName := r.FormValue("rule_name")
// Fetch rule from database
row := db.QueryRow(`SELECT listen_address, listen_port FROM rules WHERE rule_name = ?`, ruleName)
var listenAddress, listenPort string
err := row.Scan(&listenAddress, &listenPort)
if err != nil {
http.Error(w, "Failed to find rule in database", http.StatusInternalServerError)
return
}
// Remove from database
_, err = db.Exec(`DELETE FROM rules WHERE rule_name = ?`, ruleName)
if err != nil {
http.Error(w, "Failed to remove rule from database", http.StatusInternalServerError)
return
}
// Remove portproxy rule
removePortProxy := exec.Command("netsh", "interface", "portproxy", "delete", "v4tov4",
"listenaddress="+listenAddress, "listenport="+listenPort)
if err := removePortProxy.Run(); err != nil {
http.Error(w, "Failed to remove port proxy", http.StatusInternalServerError)
return
}
// Remove firewall rule
removeFirewall := exec.Command("netsh", "advfirewall", "firewall", "delete", "rule",
"name="+ruleName)
if err := removeFirewall.Run(); err != nil {
http.Error(w, "Failed to remove firewall rule", http.StatusInternalServerError)
return
}
listHandler(w, r)
}
}
// List all rules
func listHandler(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query(`SELECT rule_name, listen_address, listen_port, connect_address, connect_port FROM rules`)
if err != nil {
http.Error(w, "Failed to fetch rules from database", http.StatusInternalServerError)
return
}
defer rows.Close()
var rules []PortProxy
for rows.Next() {
var rule PortProxy
if err := rows.Scan(&rule.RuleName, &rule.ListenAddress, &rule.ListenPort, &rule.ConnectAddress, &rule.ConnectPort); err != nil {
http.Error(w, "Failed to parse rules", http.StatusInternalServerError)
return
}
rules = append(rules, rule)
}
if err := templates.ExecuteTemplate(w, "list.html", rules); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func getRouterIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "127.0.0.1"
}
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
ip := ipNet.IP.String()
// Verify if it's in a private IP range (e.g., 192.168.x.x)
if strings.HasPrefix(ip, "192.168.") || strings.HasPrefix(ip, "10.") || strings.HasPrefix(ip, "172.") {
return ip
}
}
}
return "127.0.0.1"
}