diff --git a/obfs4proxy/obfs4proxy.go b/obfs4proxy/obfs4proxy.go index 9a9083d..3ad8785 100644 --- a/obfs4proxy/obfs4proxy.go +++ b/obfs4proxy/obfs4proxy.go @@ -69,7 +69,6 @@ const ( obfs4LogFile = "obfs4proxy.log" ) -var enableLogging bool var unsafeLogging bool var ptListeners []net.Listener @@ -173,8 +172,6 @@ func serverSetup() bool { return launch } - ptInitializeLogging(ptServerInfo.StateLocation) - for _, bindaddr := range ptServerInfo.Bindaddrs { switch bindaddr.MethodName { case obfs4Method: @@ -292,8 +289,6 @@ func clientSetup() bool { return launch } - ptInitializeLogging(ptClientInfo.StateLocation) - for _, methodName := range ptClientInfo.MethodNames { switch methodName { case obfs4Method: @@ -315,8 +310,37 @@ func clientSetup() bool { return launch } -func ptInitializeLogging(dir string) { - if enableLogging { +func ptIsClient() bool { + env := os.Getenv("TOR_PT_CLIENT_TRANSPORTS") + return env != "" +} + +func ptIsServer() bool { + env := os.Getenv("TOR_PT_SERVER_TRANSPORTS") + return env != "" +} + +func ptGetStateDir() (dir string, err error) { + dir = os.Getenv("TOR_PT_STATE_LOCATION") + if dir == "" { + return + } + + err = os.MkdirAll(dir, 0755) + if err != nil { + log.Fatalf("[ERROR] Failed to create path: %s", err) + } + + return +} + +func ptInitializeLogging(enable bool) { + if enable { + dir, err := ptGetStateDir() + if err != nil || dir == "" { + return + } + f, err := os.OpenFile(path.Join(dir, obfs4LogFile), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) if err != nil { log.Fatalf("[ERROR] Failed to open log file: %s", err) @@ -370,7 +394,7 @@ func generateServerParams(id string) { func main() { // Some command line args. genParams := flag.String("genServerParams", "", "Generate server params given a bridge fingerprint.") - flag.BoolVar(&enableLogging, "enableLogging", false, "Log to TOR_PT_STATE_LOCATION/obfs4proxy.log") + doLogging := flag.Bool("enableLogging", false, "Log to TOR_PT_STATE_LOCATION/obfs4proxy.log") flag.BoolVar(&unsafeLogging, "unsafeLogging", false, "Disable the address scrubber") flag.Parse() if *genParams != "" { @@ -378,11 +402,14 @@ func main() { os.Exit(0) } + // Initialize pt logging. + ptInitializeLogging(*doLogging) + // Go through the pt protocol and initialize client or server mode. launched := false - if pt.IsClient() { + if ptIsClient() { launched = clientSetup() - } else if pt.IsServer() { + } else if ptIsServer() { launched = serverSetup() } if !launched {