From: qydysky Date: Thu, 30 Jul 2020 03:52:41 +0000 (+0800) Subject: 12 X-Git-Tag: v0.0.2~76 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=7757243c78cd93a287de22b5d1f097dec018935c;p=part%2F.git 12 --- diff --git a/Exec.go b/Exec.go new file mode 100644 index 0000000..7ce2865 --- /dev/null +++ b/Exec.go @@ -0,0 +1,111 @@ +package part + +import ( + "os" + "bufio" + // "fmt" + Ppart "github.com/qydysky/part/linuxwin" + "io" + "os/exec" + "errors" + "strings" +) + +func Log (cmd *exec.Cmd,filename string) error { + var newpath func(string) error = func (filename string)error{ + /* + 如果filename路径不存在,就新建它 + */ + var exist func(string) bool = func (s string) bool { + _, err := os.Stat(s) + return err == nil || os.IsExist(err) + } + + for i:=0;true;{ + a := strings.Index(filename[i:],"/") + if a == -1 {break} + if a == 0 {a = 1}//bug fix 当绝对路径时开头的/导致问题 + i=i+a+1 + if !exist(filename[:i-1]) { + err := os.Mkdir(filename[:i-1], os.ModePerm) + if err != nil {return err} + } + } + + if _, err := os.Stat(filename); os.IsNotExist(err) { + fd,err:=os.Create(filename) + if err != nil { + return err + }else{ + fd.Close() + } + } + return nil + } + if err:=newpath(filename);err != nil {return err} + + fd, err := os.OpenFile(filename, os.O_RDWR | os.O_EXCL, 0755) + if err != nil {return err} + + wb := bufio.NewWriter(fd) + + var w func(string) = func(s string) { + _, err2 := wb.WriteString(s+"\n") + if err2 != nil { + Logf().E(err2.Error()) + } + } + defer func(){ + w("Log stop!") + wb.Flush() + fd.Sync() + fd.Close() + }() + + w("Log start!") + var s string + for _,v:= range cmd.Args { + s+=v+" "; + } + w("cmd: "+s) + + stdout, err := cmd.StdoutPipe() + + if err != nil { + w(err.Error()) + return errors.New(err.Error()) + } + + Startf(cmd) + + reader := bufio.NewReader(stdout) + + for { + line, err2 := reader.ReadString('\n') + if err2 != nil || io.EOF == err2 { + break + } + w(line) + } + + if err:=cmd.Wait();err !=nil{return err} + return nil +} + +func Run(hide bool,prog string,cmd ...string){ + Ppart.PRun(hide,prog,cmd ...) +} + +func Startf(pro ...*exec.Cmd){ + Ppart.PStartf(pro) +} + +func Stop(pro ...*exec.Cmd){ + for i := range pro { + pro[i].Process.Kill() + } +} + +func Cmd () { + +} \ No newline at end of file diff --git a/Limit.go b/Limit.go new file mode 100644 index 0000000..7970bfa --- /dev/null +++ b/Limit.go @@ -0,0 +1,46 @@ +package part + +import ( + "time" +) + +type Limitl struct { + Stop bool + Max int + Second int + TimeOut int + Channl chan bool +} + +func Limit(Max,Second,TimeOut int) (*Limitl) { + + returnVal := Limitl{} + if Max < 1 || Second < 1 || TimeOut < Second{return &returnVal} + + returnVal = Limitl{ + Max:Max, + Second:Second, + TimeOut:TimeOut, + Channl:make(chan bool,Max), + } + + go func(returnVal *Limitl){ + for !returnVal.Stop { + for i:=1;i<=Max;i++{ + returnVal.Channl <- true + } + time.Sleep(time.Duration(Second)*time.Second) + } + }(&returnVal) + + return &returnVal +} + +func (l *Limitl) TO() bool { + if l.Stop {return false} + select { + case <-l.Channl :; + case <-time.After(time.Duration(l.TimeOut)*time.Second):return true; + } + return false +} \ No newline at end of file diff --git a/Lock.go b/Lock.go index 9b28540..007df31 100644 --- a/Lock.go +++ b/Lock.go @@ -1,52 +1,52 @@ package part -import ( - "os" - "sync" -) - -type lock struct { - sync.Mutex -} - -var ( - lock_file *os.File - lock_md5Key string -) - -func Lock() *lock { - return &lock{} -} - -func (l *lock) Start(filePath string) int { - l.Lock() - defer l.Unlock() - - if l.State() {return 1} - if Checkfile().IsOpen(lock_md5Key) {return 2} - if !Checkfile().IsExist(filePath) {return 3} - - fi, err := os.Stat(filePath) - if err != nil {return 4} - - lock_md5Key = ".lock." + fi.Name() - lock_file, _ = os.Create(lock_md5Key) - return 0 -} - -func (l *lock) Stop() int { - l.Lock() - defer l.Unlock() - - if !l.State() {return 1} - if !Checkfile().IsExist(lock_md5Key) {return 2} - - lock_file.Close() - os.Remove(lock_md5Key) - lock_md5Key = "" - return 0 -} - -func (*lock) State() bool { - return lock_md5Key != "" -} \ No newline at end of file +// import ( +// "os" +// "sync" +// ) + +// type lock struct { +// sync.Mutex +// } + +// var ( +// lock_file *os.File +// lock_md5Key string +// ) + +// func Lock() *lock { +// return &lock{} +// } + +// func (l *lock) Start(filePath string) int { +// l.Lock() +// defer l.Unlock() + +// if l.State() {return 1} +// if !Checkfile().IsExist(filePath) {return 3} + +// fi, err := os.Stat(filePath) +// if err != nil {return 4} +// if Sys().Check(fi.Name())[0] != 0 {return 2} + +// // lock_md5Key = ".lock." + fi.Name() +// // lock_file, _ = os.Create(lock_md5Key) +// return 0 +// } + +// func (l *lock) Stop() int { +// l.Lock() +// defer l.Unlock() + +// if !l.State() {return 1} +// if !Checkfile().IsExist(lock_md5Key) {return 2} + +// lock_file.Close() +// os.Remove(lock_md5Key) +// lock_md5Key = "" +// return 0 +// } + +// func (*lock) State() bool { +// return lock_md5Key != "" +// } \ No newline at end of file diff --git a/Log.go b/Log.go index cc9473c..ea26097 100644 --- a/Log.go +++ b/Log.go @@ -51,17 +51,17 @@ func (*logl) New(fileP string) { func (*logl) T(l ...interface{}){ if !isinit {log.Println("TRACE:",l);return} - tracef.Println(l) + tracef.Println(l...) } func (*logl) I(l ...interface{}){ if !isinit {log.Println("INFO:",l);return} - infof.Println(l) + infof.Println(l...) } func (*logl) W(l ...interface{}){ if !isinit {log.Println("WARNING:",l);return} - warningf.Println(l) + warningf.Println(l...) } func (*logl) E(l ...interface{}){ if !isinit {log.Println("ERROR:",l);return} - errorf.Println(l) + errorf.Println(l...) } \ No newline at end of file diff --git a/Port.go b/Port.go index 66112fc..8891d49 100644 --- a/Port.go +++ b/Port.go @@ -1,5 +1,9 @@ package part +import ( + "net" +) + type port struct {} var ( @@ -20,13 +24,21 @@ func (*port) Del(key string) { delete(port_map,key) } +func (*port) Set(key string,l net.Listener) { + port_buf<-true + defer func(){ + <-port_buf + }() + port_map[key] = l.Addr().(*net.TCPAddr).Port +} + func (*port) New(key string) int { port_buf<-true defer func(){ <-port_buf }() - if p := Sys().GetFreeProt();p != 0{ + if p := Sys().GetFreePort();p != 0{ port_map[key] = p return p } diff --git a/Session.go b/Session.go new file mode 100644 index 0000000..d547fe8 --- /dev/null +++ b/Session.go @@ -0,0 +1,66 @@ +package part + +import ( + "time" + "strconv" +) + +type session struct {} + +const ( + session_SumInTimeout int64 = 1e5 + session_Timeout int64 = 1 +) + +var ( + session_ks map[string]string = make(map[string]string) + session_kt map[string]int64 = make(map[string]int64) + session_rand int64 = 1 + session_now int64 + session_stop chan bool = make(chan bool,1) +) + + +func Session() (*session) {return &session{}} + +func (s *session) Set(key string) (val string) { + + session_stop <- true + + if session_rand >= session_SumInTimeout {session_rand = 1}else{session_rand += 1} + + t := strconv.FormatInt(session_rand, 10) + session_ks[t] = key + session_kt[t] = session_now + + <-session_stop + return t +} + +func (s *session) Get(val string) (ok bool,key string){ + + K, oks := session_ks[val] + if !oks {return false,""} + T, _ := session_kt[val] + + return session_now-T <= session_Timeout, K +} + +func (s *session) Check(val string,key string) bool { + ok,k := s.Get(val) + return ok && k == key +} + +func (s *session) Buf() (int64,int) { + return session_now,len(session_ks) +} + + +func init(){ + go func(){ + for{ + session_now = time.Now().Unix() + time.Sleep(time.Second) + } + }() +} \ No newline at end of file diff --git a/Sys.go b/Sys.go index bd8cb67..d55d2c7 100644 --- a/Sys.go +++ b/Sys.go @@ -8,6 +8,9 @@ import ( "time" "net" "strconv" + "io/ioutil" + + Ppart "github.com/qydysky/part/linuxwin" ) type sys struct {sync.Mutex} @@ -44,7 +47,7 @@ func (this *sys) GetTime() string { return now[len(now)-4:] } -func (this *sys) GetFreeProt() int { +func (this *sys) GetFreePort() int { addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0") if err != nil { return 0 @@ -54,8 +57,30 @@ func (this *sys) GetFreeProt() int { if err != nil { return 0 } - defer l.Close() - return l.Addr().(*net.TCPAddr).Port + p := l.Addr().(*net.TCPAddr).Port + l.Close() + return p +} + +func (t *sys) GetTmpDir() string { + defer t.Unlock() + t.Lock() + + dir, err := ioutil.TempDir("", "") + if err != nil {Logf().E(err.Error());return ""} + + return dir +} + +func (t *sys) GetTmpFile(dir string) string { + defer t.Unlock() + t.Lock() + + tmpfile, err := ioutil.TempFile(dir, "*.tmp") + if err != nil {Logf().E(err.Error());return ""} + name := tmpfile.Name() + tmpfile.Close() + return name } func (this *sys) GetIntranetIp() string { @@ -82,3 +107,11 @@ func (this *sys) GetIntranetIp() string { Logf().E("[part]no loacl ip") return "127.0.0.1" } + +func (this *sys) CheckProgram(pros ...string) []int { + return Ppart.PCheck(pros); +} + +func (this *sys) SetProxy(s,pac string) error { + return Ppart.PProxy(s,pac); +} \ No newline at end of file