mirror of
https://github.com/lwch/natpass.git
synced 2024-04-21 12:41:54 +00:00
实现code-server的连接创建逻辑
This commit is contained in:
@@ -2,25 +2,36 @@ package code
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/lwch/logging"
|
||||
"github.com/lwch/natpass/code/client/conn"
|
||||
"github.com/lwch/natpass/code/client/global"
|
||||
"github.com/lwch/natpass/code/client/rule"
|
||||
"github.com/lwch/runtime"
|
||||
)
|
||||
|
||||
// Code code-server handler
|
||||
type Code struct {
|
||||
Name string
|
||||
cfg global.Rule
|
||||
sync.RWMutex
|
||||
Name string
|
||||
cfg global.Rule
|
||||
workspace map[string]*Workspace
|
||||
readTimeout time.Duration
|
||||
writeTimeout time.Duration
|
||||
}
|
||||
|
||||
// New new code-server handler
|
||||
func New(cfg global.Rule) *Code {
|
||||
func New(cfg global.Rule, readTimeout, writeTimeout time.Duration) *Code {
|
||||
return &Code{
|
||||
Name: cfg.Name,
|
||||
cfg: cfg,
|
||||
Name: cfg.Name,
|
||||
cfg: cfg,
|
||||
workspace: make(map[string]*Workspace),
|
||||
readTimeout: readTimeout,
|
||||
writeTimeout: writeTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +50,37 @@ func (code *Code) GetPort() uint16 {
|
||||
return code.cfg.LocalPort
|
||||
}
|
||||
|
||||
// GetTarget get target of this rule
|
||||
func (code *Code) GetTarget() string {
|
||||
return code.cfg.Target
|
||||
}
|
||||
|
||||
// GetLinks get rule links
|
||||
func (code *Code) GetLinks() []rule.Link {
|
||||
ret := make([]rule.Link, 0, len(code.workspace))
|
||||
code.RLock()
|
||||
for _, link := range code.workspace {
|
||||
ret = append(ret, link)
|
||||
}
|
||||
code.RUnlock()
|
||||
return ret
|
||||
}
|
||||
|
||||
// GetRemote get remote target name
|
||||
func (code *Code) GetRemote() string {
|
||||
return code.cfg.Target
|
||||
}
|
||||
|
||||
// NewLink new link
|
||||
func (code *Code) NewLink(id, remote string, localConn net.Conn, remoteConn *conn.Conn) rule.Link {
|
||||
remoteConn.AddLink(id)
|
||||
ws := newWorkspace(code, id, code.cfg.Name, remote, remoteConn)
|
||||
code.Lock()
|
||||
code.workspace[ws.id] = ws
|
||||
code.Unlock()
|
||||
return ws
|
||||
}
|
||||
|
||||
// Handle handle code-server
|
||||
func (code *Code) Handle(c *conn.Conn) {
|
||||
defer func() {
|
||||
@@ -52,6 +94,7 @@ func (code *Code) Handle(c *conn.Conn) {
|
||||
}
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/new", pf(code.New))
|
||||
mux.HandleFunc("/", pf(code.Forward))
|
||||
svr := &http.Server{
|
||||
Addr: fmt.Sprintf("%s:%d", code.cfg.LocalAddr, code.cfg.LocalPort),
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package code
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/lwch/logging"
|
||||
"github.com/lwch/natpass/code/client/conn"
|
||||
"github.com/lwch/natpass/code/network"
|
||||
"github.com/lwch/runtime"
|
||||
)
|
||||
|
||||
// New new code-server workspace
|
||||
func (code *Code) New(conn *conn.Conn, w http.ResponseWriter, r *http.Request) {
|
||||
id, err := runtime.UUID(16, "0123456789abcdef")
|
||||
if err != nil {
|
||||
logging.Error("failed to generate link_id for code-server: %s, err=%v",
|
||||
code.Name, err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
link := code.NewLink(id, code.cfg.Target, nil, conn).(*Workspace)
|
||||
conn.SendConnectReq(id, code.cfg)
|
||||
ch := conn.ChanRead(id)
|
||||
timeout := time.After(code.readTimeout)
|
||||
var repMsg *network.Msg
|
||||
for {
|
||||
var msg *network.Msg
|
||||
select {
|
||||
case msg = <-ch:
|
||||
case <-timeout:
|
||||
logging.Error("create code-server %s by rule %s failed, timtout", link.id, link.parent.Name)
|
||||
http.Error(w, "timeout", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
if msg.GetXType() != network.Msg_connect_rep {
|
||||
conn.Reset(id, msg)
|
||||
time.Sleep(code.readTimeout / 10)
|
||||
continue
|
||||
}
|
||||
rep := msg.GetCrep()
|
||||
if !rep.GetOk() {
|
||||
logging.Error("create code-server %s by rule %s failed, err=%s",
|
||||
link.id, link.parent.Name, rep.GetMsg())
|
||||
http.Error(w, rep.GetMsg(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
repMsg = msg
|
||||
break
|
||||
}
|
||||
logging.Info("create link %s for code-server rule [%s] from %s to %s",
|
||||
link.GetID(), code.cfg.Name,
|
||||
repMsg.GetTo(), repMsg.GetFrom())
|
||||
fmt.Fprint(w, id)
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package code
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/lwch/logging"
|
||||
"github.com/lwch/natpass/code/client/conn"
|
||||
)
|
||||
|
||||
// Workspace workspace of code-server
|
||||
type Workspace struct {
|
||||
parent *Code
|
||||
id string
|
||||
target string
|
||||
name string
|
||||
exec *exec.Cmd
|
||||
remote *conn.Conn
|
||||
// runtime
|
||||
sendBytes uint64
|
||||
recvBytes uint64
|
||||
sendPacket uint64
|
||||
recvPacket uint64
|
||||
}
|
||||
|
||||
func newWorkspace(parent *Code, id, name, target string, remote *conn.Conn) *Workspace {
|
||||
name = strings.ReplaceAll(name, "/", "_")
|
||||
name = strings.ReplaceAll(name, "\\", "_")
|
||||
return &Workspace{
|
||||
parent: parent,
|
||||
id: id,
|
||||
target: target,
|
||||
name: name,
|
||||
remote: remote,
|
||||
}
|
||||
}
|
||||
|
||||
// GetID get link id
|
||||
func (ws *Workspace) GetID() string {
|
||||
return ws.id
|
||||
}
|
||||
|
||||
// GetBytes get send and recv bytes
|
||||
func (ws *Workspace) GetBytes() (uint64, uint64) {
|
||||
return ws.recvBytes, ws.sendBytes
|
||||
}
|
||||
|
||||
// GetPackets get send and recv packets
|
||||
func (ws *Workspace) GetPackets() (uint64, uint64) {
|
||||
return ws.recvPacket, ws.sendPacket
|
||||
}
|
||||
|
||||
// Exec execute code-server
|
||||
func (ws *Workspace) Exec(dir string) error {
|
||||
workdir := filepath.Join(dir, ws.name)
|
||||
err := os.MkdirAll(workdir, 0755)
|
||||
if err != nil {
|
||||
logging.Error("can not create work dir[%s]: %v", workdir, err)
|
||||
return err
|
||||
}
|
||||
ws.exec = exec.Command("code-server", "--disable-update-check",
|
||||
"--auth", "none",
|
||||
"--socket", filepath.Join(workdir, ws.id+".sock"),
|
||||
"--user-data-dir", filepath.Join(workdir, "data"),
|
||||
"--extensions-dir", filepath.Join(workdir, "extensions"))
|
||||
stdout, err := ws.exec.StdoutPipe()
|
||||
if err != nil {
|
||||
logging.Error("can not get stdout pipe for link [%s] name [%s]", ws.id, ws.name)
|
||||
return err
|
||||
}
|
||||
stderr, err := ws.exec.StderrPipe()
|
||||
if err != nil {
|
||||
logging.Error("can not get stderr pipe for link [%s] name [%s]", ws.id, ws.name)
|
||||
return err
|
||||
}
|
||||
err = ws.exec.Start()
|
||||
if err != nil {
|
||||
logging.Error("can not start code-server for link [%s] name [%s]", ws.id, ws.name)
|
||||
return err
|
||||
}
|
||||
go ws.log(stdout, stderr)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close close workspace
|
||||
func (ws *Workspace) Close() {
|
||||
if ws.exec != nil && ws.exec.Process != nil {
|
||||
ws.exec.Process.Kill()
|
||||
}
|
||||
ws.remote.SendDisconnect(ws.target, ws.id)
|
||||
}
|
||||
|
||||
func (ws *Workspace) log(stdout, stderr io.ReadCloser) {
|
||||
defer stdout.Close()
|
||||
defer stderr.Close()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
|
||||
watch := func(target io.Reader) {
|
||||
defer wg.Done()
|
||||
s := bufio.NewScanner(target)
|
||||
for s.Scan() {
|
||||
logging.Info("code-server [%s] [%s]: %s", ws.id, ws.name, s.Text())
|
||||
}
|
||||
}
|
||||
|
||||
go watch(stdout)
|
||||
go watch(stderr)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (ws *Workspace) Forward() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user