Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
use kingpin instead of flag, add curl support
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Aug 22, 2018
1 parent d9eb9cf commit 6b814a4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
2 changes: 1 addition & 1 deletion dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"time"

"github.com/franela/goreq"
"github.com/codeskyblue/goreq"
"github.com/miekg/dns"
)

Expand Down
69 changes: 36 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
Expand All @@ -29,13 +28,15 @@ import (
"syscall"
"time"

"github.com/alecthomas/kingpin"
"github.com/codeskyblue/goreq"
"github.com/codeskyblue/procfs"
"github.com/franela/goreq"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/mholt/archiver"
"github.com/openatx/androidutils"
"github.com/openatx/atx-agent/cmdctrl"
"github.com/openatx/atx-agent/subcmd"
"github.com/pkg/errors"
"github.com/qiniu/log"
"github.com/rs/cors"
Expand All @@ -54,10 +55,11 @@ var (
},
}

version = "dev"
owner = "openatx"
repo = "atx-agent"
listenPort int
version = "dev"
owner = "openatx"
repo = "atx-agent"
listenPort int
daemonLogPath = "/sdcard/atx-agent.log"
)

const (
Expand Down Expand Up @@ -1317,13 +1319,10 @@ func (s *Server) Serve(lis net.Listener) error {
return s.httpServer.Serve(lis)
}

func runDaemon(logPath string) (cntxt *daemon.Context) {
if logPath == "" {
logPath = "/sdcard/atx-agent.log"
}
func runDaemon() (cntxt *daemon.Context) {
cntxt = &daemon.Context{ // remove pid to prevent resource busy
PidFilePerm: 0644,
LogFileName: logPath,
LogFileName: daemonLogPath,
LogFilePerm: 0640,
WorkDir: "./",
Umask: 022,
Expand All @@ -1346,23 +1345,27 @@ func stopSelf() {
log.Println("wait server stopped")
time.Sleep(1000 * time.Millisecond) // server will quit in 0.1s
} else {
log.Println(err)
log.Println("already stopped")
}
}

func main() {
fDaemon := flag.Bool("d", false, "run daemon")
flag.IntVar(&listenPort, "p", 7912, "listen port") // Create on 2017/09/12
fVersion := flag.Bool("v", false, "show version")
fRequirements := flag.Bool("r", false, "install minicap and uiautomator.apk")
fStop := flag.Bool("stop", false, "stop server")
fLog := flag.String("log", "", "log path")
fTunnelServer := flag.String("t", "", "tunnel server address")
fNoUiautomator := flag.Bool("nouia", false, "not start uiautomator")
flag.Parse()

if *fVersion {
fmt.Println(version)
fDaemon := kingpin.Flag("daemon", "run in daemon mode").Short('d').Bool()
kingpin.Flag("port", "listen port").Default("7912").Short('p').IntVar(&listenPort) // Create on 2017/09/12
fStop := kingpin.Flag("stop", "stop server").Bool()
kingpin.Flag("log", "log file path when in daemon mode").StringVar(&daemonLogPath)
fTunnelServer := kingpin.Flag("server", "server url").Short('t').String()
fNoUiautomator := kingpin.Flag("nouia", "do not start uiautoamtor when start").Bool()
kingpin.Version(version)
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.CommandLine.VersionFlag.Short('v')

curl := kingpin.Command("curl", "simulate curl command")
subcmd.RegisterCurl(curl)

switch kingpin.Parse() {
case "curl":
subcmd.DoCurl()
return
}

Expand All @@ -1380,22 +1383,22 @@ func main() {
return
}

if *fRequirements {
log.Println("check dependencies")
if err := installRequirements(); err != nil {
// panic(err)
log.Println("requirements not ready:", err)
return
}
}
// if *fRequirements {
// log.Println("check dependencies")
// if err := installRequirements(); err != nil {
// // panic(err)
// log.Println("requirements not ready:", err)
// return
// }
// }

if _, err := os.Stat("/sdcard/tmp"); err != nil {
os.MkdirAll("/sdcard/tmp", 0755)
}
os.Setenv("TMPDIR", "/sdcard/tmp")

if *fDaemon {
cntxt := runDaemon(*fLog)
cntxt := runDaemon()
if cntxt == nil {
log.Printf("atx-agent listening on %v:%d", mustGetOoutboundIP(), listenPort)
return
Expand Down
3 changes: 2 additions & 1 deletion tunnelproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"
"time"

"github.com/codeskyblue/goreq"
"github.com/codeskyblue/heartbeat"
"github.com/franela/goreq"
"github.com/openatx/androidutils"
"github.com/openatx/atx-server/proto"
)
Expand Down Expand Up @@ -130,6 +130,7 @@ func (t *TunnelProxy) checkUpdate() error {
// return err
// }
// log.Printf("restarting server")
// os.Setenv(daemon.MARK_NAME, daemon.MARK_VALUE+":reset")
// runDaemon()
// os.Exit(0)
// }
Expand Down
2 changes: 1 addition & 1 deletion update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"strconv"
"strings"

"github.com/franela/goreq"
"github.com/codeskyblue/goreq"
"github.com/getlantern/go-update"
"github.com/mholt/archiver"
"github.com/mitchellh/ioprogress"
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"strings"
"time"

"github.com/codeskyblue/goreq"
"github.com/codeskyblue/procfs"
"github.com/franela/goreq"
shellquote "github.com/kballard/go-shellquote"
"github.com/openatx/androidutils"
"github.com/shogo82148/androidbinary/apk"
Expand Down

0 comments on commit 6b814a4

Please sign in to comment.