Skip to content

Commit

Permalink
only use logfile if there's no tty
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzorin committed Jun 9, 2018
1 parent ab81111 commit a0c08b7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ It works in the following way:

```bash
# Download it once to your Linux system
sudo curl -L -o /usr/sbin/acme-alpn-proxy "https://github.com/letsdebug/acme-alpn-proxy/releases/download/0.1.0/acme-alpn-proxy"
sudo curl -L -o /usr/sbin/acme-alpn-proxy "https://github.com/letsdebug/acme-alpn-proxy/releases/download/0.2.0/acme-alpn-proxy"

# Invoke your ACME client
# This is a speculative example, the standalone authenticator in Certbot does not yet support TLS-ALPN-01
Expand Down
35 changes: 23 additions & 12 deletions acme-alpn-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
)

const ()
Expand Down Expand Up @@ -46,20 +48,23 @@ func main() {
iptablesRuleSpec = s
}

logFile, err := os.OpenFile("/var/log/acme-alpn-proxy.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
if err != nil {
log.Fatalf("Failed to open log file for writing: %v", err)
}
defer logFile.Close()
// Redirect stdout and stderr if we're running inside something like Certbot
if !isTTY(os.Stdout.Fd()) || !isTTY(os.Stderr.Fd()) {
logFile, err := os.OpenFile("/var/log/acme-alpn-proxy.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
if err != nil {
log.Fatalf("Failed to open log file for writing: %v", err)
}
defer logFile.Close()

// Close all fds so Certbot's Popen doesn't block on them
os.Stdout.Close()
os.Stderr.Close()
// Close all fds so Certbot's Popen doesn't block on them
os.Stdout.Close()
os.Stderr.Close()

// Log to file
os.Stderr = logFile
os.Stdout = logFile
log.SetOutput(logFile)
// Log to file
os.Stderr = logFile
os.Stdout = logFile
log.SetOutput(logFile)
}

// Program must be invoked with stop or start as its only non-flag argument
op := flag.Arg(0)
Expand Down Expand Up @@ -378,3 +383,9 @@ func alreadyRunning() (bool, int, error) {

return strings.Contains(string(statusFile), filepath.Base(os.Args[0])), pid, nil
}

func isTTY(fd uintptr) bool {
var termios syscall.Termios
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, syscall.TCGETS, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
return err == 0
}

0 comments on commit a0c08b7

Please sign in to comment.