@@ -6,56 +6,59 @@ import (
66	"app/library/types/jsonutil" 
77	"errors" 
88	"fmt" 
9+ 	"github.com/gin-gonic/gin" 
910	"github.com/imroc/req" 
11+ 	"gorm.io/gorm" 
1012	"net/http" 
1113	"os/exec" 
12- 	"sync" 
14+ 	"strings" 
15+ 	"syscall" 
1316	"time" 
1417)
1518
16- var  ttyPorts  sync.Map 
19+ const  (
20+ 	TTY_PORT_START  =  40000 
21+ )
22+ 
23+ type  TtyPort  struct  {
24+ 	ID         uint32          `gorm:"primarykey;column:id" json:"id" form:"id"` 
25+ 	Uid        uint32          `gorm:"" json:"-" form:"-"` 
26+ 	Port       uint32          `gorm:"" json:"-" form:"-"` 
27+ 	Cmd        string          `gorm:"" json:"-" form:"-"` 
28+ 	CreatedAt  time.Time       `json:"createdAt"` 
29+ 	UpdatedAt  time.Time       `json:"updatedAt"` 
30+ 	DeletedAt  gorm.DeletedAt  `gorm:"index" json:"-"` 
31+ }
1732
18- func  KillPortProcess (port  int ) error  {
19- 	return  nil 
20- 	//i, ok := ttyPorts.Load(port) 
21- 	//if !ok { 
22- 	//	return nil 
23- 	//} 
24- 	//md5ID, ok := i.(string) 
25- 	//if !ok || md5ID == "" { 
26- 	//	return nil 
27- 	//} 
28- 	//cmd := exec.Command("sh", "-c", fmt.Sprintf(`ps aux | grep -e "MD5=%s" | grep -v grep | awk '{print $1}' | sort -rn | sed -n '1p' | xargs kill`, md5ID)) 
29- 	//bytes, err := cmd.CombinedOutput() 
30- 	//fmt.Println(cmd.Args, string(bytes)) 
31- 	//return err 
33+ func  (t  * TtyPort ) TableName () string  {
34+ 	return  "d_tty_port" 
3235}
3336
34- func  CreateGoTTY (writeFlag  bool , md5ID  string , arg  ... string ) (port  int , err  error ) {
35- 	port  =  40000 
36- 	for  {
37- 		if  _ , ok  :=  ttyPorts .Load (port ); ok  {
38- 			port ++ 
37+ func  CreateGoTTY (c  * gin.Context , writeFlag  bool , arg  ... string ) (port  uint32 , err  error ) {
38+ 	//gotty port save 
39+ 	err  =  DB ().Transaction (func (tx  * gorm.DB ) error  {
40+ 		var  find  TtyPort 
41+ 		if  tx .Order ("port DESC" ).First (& find ).Error  ==  nil  {
42+ 			port  =  find .Port  +  1 
3943		} else  {
40- 			break 
44+ 			port   =   TTY_PORT_START 
4145		}
42- 	}
43- 	if  port  >  50000  {
44- 		err  =  errors .New ("TTY No Port Can Used" )
46+ 		//create tty args 
47+ 		var  appendArg  =  []string {}
48+ 		if  writeFlag  {
49+ 			appendArg  =  append (appendArg , "-w" )
50+ 		}
51+ 		appendArg  =  append (appendArg , "-p" , convert .MustString (port ), "--once" , "--timeout" , "15" )
52+ 		arg  =  append (appendArg , arg ... )
53+ 		//save tty port 
54+ 		return  tx .Save (& TtyPort {Uid : UID (c ), Port : port , Cmd : strings .Join (append ([]string {"gotty" }, arg ... ), " " )}).Error 
55+ 	})
56+ 	if  err  !=  nil  {
4557		return 
4658	}
47- 	ttyPorts .Store (port , md5ID )
48- 	var  appendArg  =  []string {}
49- 	if  writeFlag  {
50- 		appendArg  =  append (appendArg , "-w" )
51- 	}
52- 	appendArg  =  append (appendArg , "-p" , convert .MustString (port ), "--once" , "--timeout" , "10" )
53- 	arg  =  append (appendArg , arg ... )
54- 
59+ 	//create gotty process 
5560	var  waitChan  =  make (chan  int , 1 )
5661	defer  close (waitChan )
57- 
58- 	//create gotty process 
5962	var  cmd  * exec.Cmd 
6063	go  func () {
6164		defer  func () {
@@ -65,7 +68,8 @@ func CreateGoTTY(writeFlag bool, md5ID string, arg ...string) (port int, err err
6568		}()
6669		cmd  =  exec .Command ("gotty" , arg ... )
6770		log .StdOut ("gotty" , port , "end" , cmd .Run ())
68- 		ttyPorts .Delete (port ) //delete port maps 
71+ 		time .Sleep (time .Second )                            //wait for gotty process finish 
72+ 		DB ().Unscoped ().Delete (& TtyPort {}, "port=?" , port ) //delete port maps 
6973	}()
7074	//test connect 
7175	var  testing  =  true 
@@ -80,10 +84,10 @@ func CreateGoTTY(writeFlag bool, md5ID string, arg ...string) (port int, err err
8084				waitChan  <-  1 
8185				return 
8286			}
83- 			if  time .Now ().After (startTime .Add (time .Second  *  3 )) { //timeout 
84- 				err  =  errors .New ("timeout" )
87+ 			if  time .Now ().After (startTime .Add (time .Second  *  10 )) { //timeout 
88+ 				err  =  errors .New ("wait gotty  timeout" )
8589				if  cmd  !=  nil  &&  cmd .Process  !=  nil  {
86- 					log .StdWarning ("gotty" , "timeout killed" , cmd .Process .Kill ())
90+ 					log .StdWarning ("gotty" , "timeout killed" , cmd .Process .Signal ( syscall . SIGINT ),  cmd . Process . Kill ())
8791				}
8892				waitChan  <-  1 
8993				return 
0 commit comments