Skip to content

Commit 8a43914

Browse files
added daemon mode
1 parent d778eac commit 8a43914

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

cmd/gohpts/cli.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func root(args []string) error {
4343
flags.StringVar(&conf.CertFile, "c", "", "Path to certificate PEM encoded file")
4444
flags.StringVar(&conf.KeyFile, "k", "", "Path to private key PEM encoded file")
4545
flags.StringVar(&conf.ServerConfPath, "f", "", "Path to server configuration file in YAML format")
46+
daemon := flags.Bool("D", false, "Run as a daemon (provide -logfile to see logs)")
4647
if runtime.GOOS == tproxyOS {
4748
flags.StringVar(&conf.TProxy, "t", "", "Address of transparent proxy server (it starts along with HTTP proxy server)")
4849
flags.StringVar(&conf.TProxyOnly, "T", "", "Address of transparent proxy server (no HTTP)")
@@ -55,14 +56,9 @@ func root(args []string) error {
5556
return nil
5657
})
5758
}
58-
flags.BoolFunc("d", "Show logs in DEBUG mode", func(flagValue string) error {
59-
conf.Debug = true
60-
return nil
61-
})
62-
flags.BoolFunc("j", "Show logs in JSON format", func(flagValue string) error {
63-
conf.Json = true
64-
return nil
65-
})
59+
flags.StringVar(&conf.LogFilePath, "logfile", "", "Log file path (Default: stdout")
60+
flags.BoolVar(&conf.Debug, "d", false, "Show logs in DEBUG mode")
61+
flags.BoolVar(&conf.Json, "j", false, "Show logs in JSON format")
6662
flags.BoolFunc("v", "print version", func(flagValue string) error {
6763
fmt.Println(gohpts.Version)
6864
os.Exit(0)
@@ -112,6 +108,11 @@ func root(args []string) error {
112108
}
113109
}
114110
}
111+
if seen["D"] {
112+
if seen["u"] || seen["U"] {
113+
return fmt.Errorf("-u and -U flags do not work in daemon mode")
114+
}
115+
}
115116
if seen["u"] {
116117
fmt.Print("SOCKS5 Password: ")
117118
bytepw, err := term.ReadPassword(int(os.Stdin.Fd()))
@@ -131,6 +132,40 @@ func root(args []string) error {
131132
fmt.Print("\033[2K\r")
132133
}
133134

135+
if *daemon {
136+
if os.Getenv("GOHPTS_DAEMON") != "1" {
137+
env := os.Environ()
138+
files := [3]*os.File{}
139+
env = append(env, "GOHPTS_DAEMON=1")
140+
files[0], _ = os.Open(os.DevNull)
141+
files[1], _ = os.Open(os.DevNull)
142+
files[2], _ = os.Open(os.DevNull)
143+
attr := &os.ProcAttr{
144+
Files: []*os.File{
145+
files[0], // stdin
146+
files[1], // stdout
147+
files[2], // stderr
148+
},
149+
Dir: ".",
150+
Env: env,
151+
}
152+
path, err := os.Executable()
153+
if err != nil {
154+
return err
155+
}
156+
process, err := os.StartProcess(
157+
path,
158+
os.Args,
159+
attr,
160+
)
161+
if err != nil {
162+
return err
163+
}
164+
fmt.Println("gohpts pid:", process.Pid)
165+
process.Release()
166+
os.Exit(0)
167+
}
168+
}
134169
p := gohpts.New(&conf)
135170
p.Run()
136171
return nil

gohpts.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,6 @@ func (p *proxyapp) Run() {
696696
type Config struct {
697697
AddrHTTP string
698698
AddrSOCKS string
699-
Debug bool
700-
Json bool
701699
User string
702700
Pass string
703701
ServerUser string
@@ -708,20 +706,25 @@ type Config struct {
708706
TProxy string
709707
TProxyOnly string
710708
TProxyMode string
709+
LogFilePath string
710+
Debug bool
711+
Json bool
711712
}
712713

713714
type logWriter struct {
715+
file *os.File
714716
}
715717

716718
func (writer logWriter) Write(bytes []byte) (int, error) {
717-
return fmt.Print(fmt.Sprintf("%s | ERROR | %s", time.Now().Format(time.RFC3339), string(bytes)))
719+
return fmt.Fprintf(writer.file, fmt.Sprintf("%s | ERROR | %s", time.Now().Format(time.RFC3339), string(bytes)))
718720
}
719721

720722
type jsonLogWriter struct {
723+
file *os.File
721724
}
722725

723726
func (writer jsonLogWriter) Write(bytes []byte) (int, error) {
724-
return fmt.Print(fmt.Sprintf("{\"level\":\"error\",\"time\":\"%s\",\"message\":\"%s\"}\n",
727+
return fmt.Fprintf(writer.file, fmt.Sprintf("{\"level\":\"error\",\"time\":\"%s\",\"message\":\"%s\"}\n",
725728
time.Now().Format(time.RFC3339), strings.TrimRight(string(bytes), "\n")))
726729
}
727730

@@ -782,14 +785,24 @@ func expandPath(p string) string {
782785
func New(conf *Config) *proxyapp {
783786
var logger zerolog.Logger
784787
var p proxyapp
788+
var logfile *os.File = os.Stdout
789+
if conf.LogFilePath != "" {
790+
f, err := os.OpenFile(conf.LogFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
791+
if err != nil {
792+
log.Fatalf("Failed to open log file: %v", err)
793+
}
794+
logfile = f
795+
}
785796
if conf.Json {
786797
log.SetFlags(0)
787-
log.SetOutput(new(jsonLogWriter))
788-
logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
798+
jsonWriter := jsonLogWriter{file: logfile}
799+
log.SetOutput(jsonWriter)
800+
logger = zerolog.New(logfile).With().Timestamp().Logger()
789801
} else {
790802
log.SetFlags(0)
791-
log.SetOutput(new(logWriter))
792-
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339, NoColor: true}
803+
logWriter := logWriter{file: logfile}
804+
log.SetOutput(logWriter)
805+
output := zerolog.ConsoleWriter{Out: logfile, TimeFormat: time.RFC3339, NoColor: true}
793806
output.FormatLevel = func(i any) string {
794807
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
795808
}

0 commit comments

Comments
 (0)