-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrp-auth-plugin.go
More file actions
192 lines (165 loc) · 4.7 KB
/
frp-auth-plugin.go
File metadata and controls
192 lines (165 loc) · 4.7 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
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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/nyu-rts/frp-auth-plugin/internal/configfile"
)
var ConfigFile configfile.ConfigProvider
func main() {
ctx := context.Background()
// Read command line
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: frp-auth-plugin <configuration-file>\n")
os.Exit(2)
}
configFileName := os.Args[1]
// Read configuration
var err error
ConfigFile, err = configfile.New(configFileName, ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading config file: %s\n", err)
os.Exit(1)
}
// Read listen address
listenAddr := os.Getenv("FRP_AUTH_PLUGIN_LISTEN_ADDR")
if listenAddr == "" {
fmt.Fprintf(os.Stderr, "FRP_AUTH_PLUGIN_LISTEN_ADDR is not set\n")
os.Exit(2)
}
// Create HTTP server
mux := http.NewServeMux()
mux.HandleFunc("POST /handler", handleReq)
server := http.Server{
Addr: listenAddr,
Handler: mux,
}
context.AfterFunc(ctx, func() { server.Close() })
log.Printf("Listening on %s", listenAddr)
err = server.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}
type FrpLogin struct {
User string `json:"user"`
PrivilegeKey string `json:"privilege_key"`
RunId string `json:"run_id"`
Metas map[string]string `json:"metas"`
ClientAddress string `json:"client_address"`
}
type FrpLoginRequest struct {
Content FrpLogin `json:"content"`
}
type FrpNewProxyUser struct {
User string `json:"user"`
Metas map[string]string `json:"metas"`
}
type FrpNewProxy struct {
User FrpNewProxyUser `json:"user"`
Metas map[string]string `json:"metas"`
ProxyName string `json:"proxy_name"`
ProxyType string `json:"proxy_type"`
RemotePort int `json:"remote_port"`
CustomDomains []string `json:"custom_domains"`
HttpUser string `json:"http_user,omitempty"`
HttpPassword string `json:"http_pwd,omitempty"`
}
type FrpNewProxyRequest struct {
Content FrpNewProxy `json:"content"`
}
func handleReq(res http.ResponseWriter, req *http.Request) {
// Read query parameters
query := req.URL.Query()
op := query.Get("op")
reject := func(reason string) {
res.WriteHeader(200)
_, _ = fmt.Fprintf(res, "{\"reject\": true, \"reject_reason\": \"%s\"}", reason)
}
// Read body
decoder := json.NewDecoder(req.Body)
// Handle operation
switch op {
case "":
log.Printf("Missing 'op' in URL")
http.Error(res, "Missing 'op' in URL", 400)
return
case "Login":
var body FrpLoginRequest
if err := decoder.Decode(&body); err != nil {
log.Printf("Bad JSON in request: %s", err)
http.Error(res, "Bad JSON", 400)
return
}
// Lookup user
config := ConfigFile.CurrentConfig()
user, ok := config.Users[body.Content.User]
if !ok {
log.Printf("Invalid user %#v", body.Content.User)
reject("invalid user")
return
}
if user.Password != body.Content.Metas["token"] {
log.Printf("Invalid password for %s", body.Content.User)
reject("invalid password")
return
}
log.Printf("Login from %s as %s", body.Content.ClientAddress, body.Content.User)
res.WriteHeader(200)
_, _ = io.WriteString(res, "{\"reject\": false, \"unchange\": true}")
case "NewProxy":
var body FrpNewProxyRequest
if err := decoder.Decode(&body); err != nil {
log.Printf("Bad JSON in request: %s", err)
http.Error(res, "Bad JSON", 400)
return
}
// Lookup user
config := ConfigFile.CurrentConfig()
user, ok := config.Users[body.Content.User.User]
if !ok {
log.Printf("Invalid user %#v", body.Content.User)
reject("invalid user")
return
}
// Lookup proxy
proxyName := body.Content.ProxyName
if strings.HasPrefix(proxyName, body.Content.User.User+".") {
proxyName = proxyName[len(body.Content.User.User)+1:]
}
proxy, ok := user.Proxies[proxyName]
if !ok {
log.Printf("Invalid proxy %s %s", body.Content.User.User, body.Content.ProxyName)
return
}
// Replace proxy config with our own
newProxy := FrpNewProxy{
User: body.Content.User,
Metas: body.Content.Metas,
ProxyName: body.Content.ProxyName,
ProxyType: body.Content.ProxyType,
CustomDomains: proxy.CustomDomains,
HttpUser: proxy.HttpUser,
HttpPassword: proxy.HttpPassword,
}
log.Printf("NewProxy %s from %s", body.Content.ProxyName, body.Content.User.User)
res.WriteHeader(200)
if _, err := io.WriteString(res, "{\"reject\": false, \"unchange\": false, \"content\":"); err != nil {
return
}
encoder := json.NewEncoder(res)
if err := encoder.Encode(newProxy); err != nil {
return
}
_, _ = io.WriteString(res, "}")
default:
res.WriteHeader(200)
_, _ = io.WriteString(res, "{\"reject\": false, \"unchange\": true}")
}
}