Browse Source

fix: user locked screen and local proxy

master
pcong 5 months ago
parent
commit
6884472130
  1. 11
      godo/model/local_proxy.go
  2. 66
      godo/proxy/local.go
  3. 20
      godo/user/user.go

11
godo/model/local_proxy.go

@ -2,12 +2,11 @@ package model
type LocalProxy struct {
BaseModel
Port uint `json:"port"` // 本地端口
ProxyType string `json:"proxyType"` // 代理类型
Domain string `json:"domain"` // 代理域名
Path string `json:"path"` // 代理路径
Status bool `json:"status"` // 状态
ListenPort uint `json:"listenPort"` // 代理端口
Port uint `json:"port"` // 本地端口
ProxyType string `json:"proxyType"` // 代理类型
Domain string `json:"domain"` // 代理域名
Path string `json:"path"` // 代理路径
Status bool `json:"status"` // 状态
}
func (*LocalProxy) TableName() string {

66
godo/proxy/local.go

@ -3,13 +3,16 @@ package proxy
import (
"context"
"encoding/json"
"errors"
"fmt"
"godo/libs"
"godo/model"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"sync"
"time"
@ -116,11 +119,10 @@ func UpdateLocalProxyHandler(w http.ResponseWriter, r *http.Request) {
return
}
updata := map[string]interface{}{
"port": lp.Port,
"proxy_type": lp.ProxyType,
"domain": lp.Domain,
"status": lp.Status,
"listen_port": lp.ListenPort,
"port": lp.Port,
"proxy_type": lp.ProxyType,
"domain": lp.Domain,
"status": lp.Status,
// path
}
@ -267,58 +269,56 @@ func stopProxy(id uint) {
default:
fmt.Printf("Unknown proxy type: %s\n", proxyServer.Type)
}
proxyServers.Delete(id)
}
}
// HTTP 代理处理函数
func httpProxyHandler(proxy model.LocalProxy) {
// 如果 ListenPort 没有传递,默认为 80
if proxy.ListenPort == 0 {
proxy.ListenPort = 80
}
fmt.Printf("Initializing HTTP proxy for ID %d on domain %s and listen port %d\n", proxy.ID, proxy.Domain, proxy.ListenPort)
// 使用 proxy.Port 作为本地目标端口
remote, err := url.Parse(fmt.Sprintf("http://localhost:%d", proxy.Port))
remote, err := url.Parse(proxy.Domain)
if err != nil {
fmt.Printf("Failed to parse remote URL for port %d: %v\n", proxy.Port, err)
return
}
fmt.Printf("Parsed remote URL: %s\n", remote.String())
if remote.Scheme == "" {
fmt.Printf("Remote URL for port %d does not contain a scheme (http/https): %s\n", proxy.Port, proxy.Domain)
return
}
// 创建反向代理
reverseProxy := httputil.NewSingleHostReverseProxy(remote)
// 设置请求头(如需要可以启用)
reverseProxy.Director = func(req *http.Request) {
req.Header.Set("X-Forwarded-Host", req.Host)
req.Header.Set("X-Origin-Host", remote.Host)
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; curl/7.68.0)") // 设置常见的 User-Agent
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.Host = remote.Host
req.URL.Path = proxy.Path + req.URL.Path // 使用代理路径
fmt.Printf("Proxying request to: %s\n", req.URL.String())
}
reverseProxy.ModifyResponse = func(resp *http.Response) error {
fmt.Printf("Received response with status: %s\n", resp.Status)
return nil
}
reverseProxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
fmt.Printf("Error during proxying: %v\n", err)
http.Error(rw, "Proxy error", http.StatusBadGateway)
}
reverseProxy.ErrorLog = log.New(os.Stderr, "proxy-debug: ", log.LstdFlags)
// 监听配置中指定的域名和端口
// 启动 HTTP 服务器并监听指定端口
bindAddress := "127.0.0.1"
serverAddr := fmt.Sprintf("%s:%d", bindAddress, proxy.Port)
server := &http.Server{
Addr: fmt.Sprintf(":%d", proxy.ListenPort),
Addr: serverAddr,
Handler: reverseProxy,
}
proxyServers.Store(proxy.ID, ProxyServer{Type: "http", Server: server})
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Printf("Failed to start HTTP proxy on domain %s and listen port %d: %v\n", proxy.Domain, proxy.ListenPort, err)
} else {
fmt.Printf("HTTP proxy on domain %s and listen port %d started successfully\n", proxy.Domain, proxy.ListenPort)
fmt.Printf("Starting HTTP proxy on LocalHost port %d and forwarding to %s \n", proxy.Port, proxy.Domain)
if err := server.ListenAndServe(); err != nil {
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("HTTP proxy on port %d stopped gracefully.\n", proxy.Port)
} else {
fmt.Printf("Error starting HTTP proxy on port %d: %v\n", proxy.Port, err)
}
}
}

20
godo/user/user.go

@ -2,6 +2,7 @@ package user
import (
"encoding/json"
"fmt"
"godo/libs"
"godo/model"
"io"
@ -74,14 +75,6 @@ func LockedScreenHandler(w http.ResponseWriter, r *http.Request) {
return
}
userEncodeStr, err := libs.EncodeFile("godoos", strconv.Itoa(int(user.ID)))
if err != nil {
libs.ErrorMsg(w, "failed to encode user")
return
}
// w.Header().Set("sysuser", userEncodeStr)
// 登录成功,锁屏状态设置为 true
mu.Lock()
if userLockedScreen == nil {
@ -90,7 +83,7 @@ func LockedScreenHandler(w http.ResponseWriter, r *http.Request) {
userLockedScreen[user.ID] = true
mu.Unlock()
libs.SuccessMsg(w, userEncodeStr, "system locked")
libs.SuccessMsg(w, fmt.Sprint(user.ID), "system locked")
}
// 系统用户登录(解锁)
@ -130,18 +123,13 @@ func CheckLockedScreenHandler(w http.ResponseWriter, r *http.Request) {
return
}
decoderUid := r.Header.Get("sysuser")
if decoderUid == "" {
uidStr := r.Header.Get("sysuser")
if uidStr == "" {
//获取锁屏状态
libs.SuccessMsg(w, false, "")
return
}
uidStr, err := libs.DecodeFile("godoos", decoderUid)
if err != nil {
libs.ErrorMsg(w, "invalid uid")
return
}
uid, err := strconv.Atoi(uidStr)
if err != nil {
libs.ErrorMsg(w, "parse uid fail")

Loading…
Cancel
Save