6 Commits

3 changed files with 47 additions and 9 deletions

31
__dist/README.txt Normal file
View File

@@ -0,0 +1,31 @@
A logging service for NMDC hubs.
It logs public chat messages to a file, categorised by months. Binaries are provided for Windows/Linux amd64/i386.
Written in golang
Tags: nmdc
=USAGE=
`$nmdc-log-service -Help
Usage of nmdc-log-service:
-Debug
Print additional information on stdout
-Dir string
Output directory (default ".")
-LogConnectionState
Include connection state changes in log (default true)
-Nick string
Nick (default "nmdc-log-service")
-PMResponse string
Message to respond with on PM (default "This is an automated service. For enquiries, please contact an administrator.")
-Password string
Registered nick password
-Server string
Addresses to connect to (comma-separated)`
=CHANGELOG=
2016-04-02 1.0.0
- Initial public release

View File

@@ -24,22 +24,22 @@ main() {
fi fi
echo "Building win64..." echo "Building win64..."
GOARCH=amd64 GOOS=windows go build -ldflags -s -o nmdc-log-service.exe GOARCH=amd64 GOOS=windows go build -a -ldflags -s -o nmdc-log-service.exe
7z a -mx9 nmdc-log-service-${version}-win64.7z nmdc-log-service.exe >/dev/null 7z a -mx9 nmdc-log-service-${version}-win64.7z nmdc-log-service.exe >/dev/null
rm ./nmdc-log-service.exe rm ./nmdc-log-service.exe
echo "Building win32..." echo "Building win32..."
GOARCH=386 GOOS=windows go build -ldflags -s -o nmdc-log-service.exe GOARCH=386 GOOS=windows go build -a -ldflags -s -o nmdc-log-service.exe
7z a -mx9 nmdc-log-service-${version}-win32.7z nmdc-log-service.exe >/dev/null 7z a -mx9 nmdc-log-service-${version}-win32.7z nmdc-log-service.exe >/dev/null
rm ./nmdc-log-service.exe rm ./nmdc-log-service.exe
echo "Building linux64..." echo "Building linux64..."
GOARCH=amd64 GOOS=linux go build -ldflags -s -o nmdc-log-service GOARCH=amd64 GOOS=linux go build -a -ldflags -s -o nmdc-log-service
XZ_OPT=-9 tar caf nmdc-log-service-${version}-linux64.tar.xz nmdc-log-service --owner=0 --group=0 XZ_OPT=-9 tar caf nmdc-log-service-${version}-linux64.tar.xz nmdc-log-service --owner=0 --group=0
rm ./nmdc-log-service rm ./nmdc-log-service
echo "Building linux32..." echo "Building linux32..."
GOARCH=386 GOOS=linux go build -ldflags -s -o nmdc-log-service GOARCH=386 GOOS=linux go build -a -ldflags -s -o nmdc-log-service
XZ_OPT=-9 tar caf nmdc-log-service-${version}-linux32.tar.xz nmdc-log-service --owner=0 --group=0 XZ_OPT=-9 tar caf nmdc-log-service-${version}-linux32.tar.xz nmdc-log-service --owner=0 --group=0
rm ./nmdc-log-service rm ./nmdc-log-service

17
main.go
View File

@@ -15,6 +15,7 @@ var BaseDir string = "."
var PMResponse string = "" var PMResponse string = ""
var LogConnectionState bool var LogConnectionState bool
var DebugMode bool var DebugMode bool
var VerifyTLS bool
var CharacterMatcher *regexp.Regexp var CharacterMatcher *regexp.Regexp
func init() { func init() {
@@ -36,7 +37,7 @@ func LogMessage(hub, message string) {
// Produce a timestamp // Produce a timestamp
timeStamp := instant.Format("2006-01-02 15:04:05") timeStamp := instant.Format("2006-01-02 15:04:05")
fh, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE, 0644) fh, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't open file '%s': %s\n", logFilePath, err.Error()) fmt.Fprintf(os.Stderr, "Couldn't open file '%s': %s\n", logFilePath, err.Error())
return return
@@ -53,9 +54,10 @@ func LogMessage(hub, message string) {
func HubWorker(addr, nick, password string) { func HubWorker(addr, nick, password string) {
opts := libnmdc.HubConnectionOptions{ opts := libnmdc.HubConnectionOptions{
Address: libnmdc.HubAddress(addr), Address: libnmdc.HubAddress(addr),
Self: libnmdc.UserInfo{Nick: nick}, SkipVerifyTLS: !VerifyTLS,
NickPassword: password, Self: libnmdc.UserInfo{Nick: nick},
NickPassword: password,
} }
hub := opts.Connect() hub := opts.Connect()
@@ -104,18 +106,23 @@ func main() {
flag.BoolVar(&LogConnectionState, "LogConnectionState", true, "Include connection state changes in log") flag.BoolVar(&LogConnectionState, "LogConnectionState", true, "Include connection state changes in log")
flag.StringVar(&PMResponse, "PMResponse", "This is an automated service. For enquiries, please contact an administrator.", "Message to respond with on PM") flag.StringVar(&PMResponse, "PMResponse", "This is an automated service. For enquiries, please contact an administrator.", "Message to respond with on PM")
flag.BoolVar(&DebugMode, "Debug", false, "Print additional information on stdout") flag.BoolVar(&DebugMode, "Debug", false, "Print additional information on stdout")
flag.BoolVar(&VerifyTLS, "VerifyTLS", true, "Verify TLS certificates")
flag.Parse() flag.Parse()
// Assert dir exists // Assert dir exists
dinfo, err := os.Stat(BaseDir) dinfo, err := os.Stat(BaseDir)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "FATAL: %s", err.Error()) fmt.Fprintf(os.Stderr, "FATAL: %s\n", err.Error())
os.Exit(1) os.Exit(1)
} }
if !dinfo.IsDir() { if !dinfo.IsDir() {
fmt.Fprintf(os.Stderr, "FATAL: Path '%s' is not a directory\n", BaseDir) fmt.Fprintf(os.Stderr, "FATAL: Path '%s' is not a directory\n", BaseDir)
} }
if !VerifyTLS {
fmt.Fprintf(os.Stderr, "WARNING: TLS certificates will not be verified!\n")
}
// Launch loggers // Launch loggers
all_hubs := strings.Split(*hubs, ",") all_hubs := strings.Split(*hubs, ",")
launch_ct := 0 launch_ct := 0