63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// LoginRequest .NET客户端发送的登录请求结构
|
|
type LoginRequest struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
// 网络服务
|
|
func Connect() {
|
|
|
|
// 初始化用户连接
|
|
http.HandleFunc("/login", loginHander)
|
|
http.HandleFunc("/servers", FrpInfo)
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
}
|
|
|
|
// LoginResponse .NET 请求头
|
|
func loginHander(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
// 解析 JSON 请求
|
|
var req LoginRequest
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
if err != nil {
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// 写入用户和密码
|
|
err = InitUser(req.Username, req.Password)
|
|
if err != nil {
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// 返回成功响应
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprintf(w, "success")
|
|
}
|
|
|
|
// 将 Frp 信息返回到 .NET 客户端
|
|
func FrpInfo(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
// 将 Frp 信息返回到 .NET 客户端
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(servers)
|
|
}
|