From f2e3a642e31da34452237d308ebb807217969672 Mon Sep 17 00:00:00 2001 From: Star Brilliant Date: Tue, 8 May 2018 21:09:27 +0800 Subject: [PATCH] Write a logger for macOS systems --- Makefile | 7 +++ darwin-wrapper/Makefile | 19 +++++++ darwin-wrapper/doh-logger.swift | 94 +++++++++++++++++++++++++++++++++ doh-client/version.go | 2 +- doh-server/version.go | 2 +- launchd/doh-client.plist | 2 + launchd/doh-server.plist | 2 + 7 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 darwin-wrapper/Makefile create mode 100644 darwin-wrapper/doh-logger.swift diff --git a/Makefile b/Makefile index 574bd5a..b2cc3ee 100644 --- a/Makefile +++ b/Makefile @@ -11,9 +11,15 @@ else endif all: doh-client/doh-client doh-server/doh-server + if [ "`uname`" = "Darwin" ]; then \ + $(MAKE) -C darwin-wrapper; \ + fi clean: rm -f doh-client/doh-client doh-server/doh-server + if [ "`uname`" = "Darwin" ]; then \ + $(MAKE) -C darwin-wrapper clean; \ + fi install: [ -e doh-client/doh-client ] || $(MAKE) doh-client/doh-client @@ -28,6 +34,7 @@ install: $(MAKE) -C systemd install "DESTDIR=$(DESTDIR)"; \ $(MAKE) -C NetworkManager install "DESTDIR=$(DESTDIR)"; \ elif [ "`uname`" = "Darwin" ]; then \ + $(MAKE) -C darwin-wrapper install "DESTDIR=$(DESTDIR)" "PREFIX=$(PREFIX)"; \ $(MAKE) -C launchd install "DESTDIR=$(DESTDIR)"; \ fi diff --git a/darwin-wrapper/Makefile b/darwin-wrapper/Makefile new file mode 100644 index 0000000..d5fa0f4 --- /dev/null +++ b/darwin-wrapper/Makefile @@ -0,0 +1,19 @@ +.PHONY: all clean install uninstall + +SWIFTC = swiftc +PREFIX = /usr/local + +all: doh-logger + +doh-logger: doh-logger.swift + $(SWIFTC) -o $@ -O $< + +clean: + rm -f doh-logger + +install: doh-logger + mkdir -p $(DESTDIR)$(PREFIX)/bin + install -m0755 doh-logger $(DESTDIR)$(PREFIX)/bin + +uninstall: + rm -f $(DESTDIR)$(PREFIX)/bin/doh-logger diff --git a/darwin-wrapper/doh-logger.swift b/darwin-wrapper/doh-logger.swift new file mode 100644 index 0000000..e394dbb --- /dev/null +++ b/darwin-wrapper/doh-logger.swift @@ -0,0 +1,94 @@ +#!/usr/bin/swift + +/* + DNS-over-HTTPS + Copyright (C) 2017-2018 Star Brilliant + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +import Foundation +import os.log + +if CommandLine.arguments.count < 3 { + let programName = CommandLine.arguments[0] + print("Usage: \(programName) LOG_NAME PROGRAM [ARGUMENTS]\n") + exit(1) +} +let logSubsystem = CommandLine.arguments[1] +let logger = OSLog(subsystem: logSubsystem, category: "default") + +let pipe = Pipe() +var buffer = Data() +NotificationCenter.default.addObserver(forName: FileHandle.readCompletionNotification, object: pipe.fileHandleForReading, queue: nil) { notification in + let data = notification.userInfo?["NSFileHandleNotificationDataItem"] as? Data ?? Data() + buffer.append(data) + var lastIndex = 0 + for (i, byte) in buffer.enumerated() { + if byte == 0x0a { + let line = String(data: buffer.subdata(in: lastIndex..org.eu.starlab.doh.client ProgramArguments + /usr/local/bin/doh-logger + doh-client /usr/local/bin/doh-client -conf /usr/local/etc/dns-over-https/doh-client.conf diff --git a/launchd/doh-server.plist b/launchd/doh-server.plist index 181e685..4be011e 100644 --- a/launchd/doh-server.plist +++ b/launchd/doh-server.plist @@ -6,6 +6,8 @@ org.eu.starlab.doh.server ProgramArguments + /usr/local/bin/doh-logger + doh-server /usr/local/bin/doh-server -conf /usr/local/etc/dns-over-https/doh-server.conf