mirror of
https://github.com/lwch/natpass.git
synced 2024-04-21 12:41:54 +00:00
优化性能
This commit is contained in:
@@ -4,13 +4,36 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/lwch/logging"
|
||||
"github.com/lwch/natpass/code/client/conn"
|
||||
)
|
||||
|
||||
// Forward forward code-server requests
|
||||
func (code *Code) Forward(conn *conn.Conn, w http.ResponseWriter, r *http.Request) {
|
||||
id := strings.TrimPrefix(r.URL.Path, "/forward/")
|
||||
id = id[:strings.Index(id, "/")]
|
||||
name := strings.TrimPrefix(r.URL.Path, "/forward/")
|
||||
name = name[:strings.Index(name, "/")]
|
||||
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/forward/"+name)
|
||||
if len(r.URL.Path) == 0 {
|
||||
r.URL.Path = "/"
|
||||
}
|
||||
|
||||
var id string
|
||||
if r.URL.Path == "/" {
|
||||
id = r.FormValue("id")
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "__NATPASS_CONNECTION_ID__",
|
||||
Value: id,
|
||||
})
|
||||
} else {
|
||||
cookie, err := r.Cookie("__NATPASS_CONNECTION_ID__")
|
||||
if err != nil {
|
||||
logging.Error("get connection id: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
id = cookie.Value
|
||||
}
|
||||
|
||||
code.RLock()
|
||||
workspace := code.workspace[id]
|
||||
@@ -21,11 +44,6 @@ func (code *Code) Forward(conn *conn.Conn, w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/forward/"+id)
|
||||
if len(r.URL.Path) == 0 {
|
||||
r.URL.Path = "/"
|
||||
}
|
||||
|
||||
if code.isWebsocket(r) {
|
||||
code.handleWebsocket(workspace, w, r)
|
||||
} else {
|
||||
|
||||
@@ -20,8 +20,8 @@ func (code *Code) handleRequest(workspace *Workspace, w http.ResponseWriter, r *
|
||||
defer workspace.closeMessage(reqID)
|
||||
resp := workspace.onResponse(reqID)
|
||||
if resp == nil {
|
||||
logging.Error("waiting for [%s] [%s] no response for request, request_id=%d",
|
||||
workspace.id, workspace.name, reqID)
|
||||
logging.Error("waiting for [%s] [%s] no response for request, uri=%s, request_id=%d",
|
||||
workspace.id, workspace.name, r.URL.Path, reqID)
|
||||
http.Error(w, "no response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -59,4 +59,5 @@ func (code *Code) handleWebsocket(workspace *Workspace, w http.ResponseWriter, r
|
||||
go workspace.ws2remote(&wg, reqID, local)
|
||||
go workspace.remote2ws(&wg, reqID, local)
|
||||
wg.Wait()
|
||||
workspace.Close()
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package code
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -52,5 +52,15 @@ func (code *Code) New(conn *conn.Conn, w http.ResponseWriter, r *http.Request) {
|
||||
link.GetID(), code.cfg.Name,
|
||||
repMsg.GetTo(), repMsg.GetFrom())
|
||||
go link.localRead()
|
||||
fmt.Fprint(w, id)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
data, err := json.Marshal(map[string]string{
|
||||
"id": id,
|
||||
"name": code.cfg.Name,
|
||||
})
|
||||
if err != nil {
|
||||
logging.Error("json.Marshal: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user