23 Commits

Author SHA1 Message Date
3a67b02bd9 libnmdc: patch wrong test 2016-04-03 13:29:34 +12:00
27c21572b3 libnmdc: use extra variable instead of reflect package 2016-04-03 13:22:09 +12:00
da92afa51e libnmdc: add SkipVerifyTLS option 2016-04-03 13:17:29 +12:00
ec0a781538 libnmdc: fix panic() on connection failure, owing to storing nil in an interface 2016-04-03 13:15:57 +12:00
052c3e2cba nmdc-log-service/build: build with -a 2016-04-03 11:50:37 +12:00
db0e1a5c2f nmdc-log-service: track readme 2016-04-02 14:31:34 +13:00
ead04a1d4d Added tag libnmdc-r2 for changeset 137c1b65039e 2016-04-02 14:28:42 +13:00
f8ac216379 libnmdc: update readme 2016-04-02 14:28:35 +13:00
28ad3c8e6a libnmdc: reintroduce sample client as part of main package 2016-04-02 14:27:48 +13:00
820fa72b77 Added tag nmdc-log-service-1.0.0 for changeset 02a360e95480 2016-04-02 14:22:24 +13:00
f4b868b0c7 Added tag libnmdc-r1 for changeset 945ab4b16d05 2016-04-02 14:22:15 +13:00
7a590755cf nmdc-log-service: buildscript 2016-04-02 14:17:56 +13:00
08d048a6d3 fix double-"default" appearing in --help output 2016-04-02 14:17:47 +13:00
a09bf67b8f nmdc-log-service: initial commit 2016-04-02 14:04:58 +13:00
d6339667fe libnmdc: fix PM detection 2016-04-02 14:01:58 +13:00
c532e2fe4f libnmdc: include error messages with EVENT_CONNECTION_STATE_CHANGED 2016-04-02 13:49:49 +13:00
433c1ddac9 libnmdc: patch panic() on nil connection 2016-04-02 13:49:38 +13:00
83792a4241 libnmdc: add default protocol/port if not present 2016-04-02 13:49:24 +13:00
2c34fe7fcb hgignore 2016-04-02 12:51:54 +13:00
786e99d743 don't track our test script 2016-04-02 12:51:37 +13:00
59ca9f8251 nmdcs support 2016-04-02 12:51:23 +13:00
4fad66857a hgignore 2016-04-02 12:51:02 +13:00
1cd8190a58 doc: track README 2016-04-02 12:50:25 +13:00
8 changed files with 341 additions and 21 deletions

View File

@@ -1,6 +1,14 @@
mode:regex
# Compilation output
\.(?:exe|a)$
^pkg/
# Dependencies
^src/(?:github.com|gopkg.in|golang.org)/
# Scratch space
^src/nmdc/
# Binary release artefacts
/?__dist/

3
.hgtags Normal file
View File

@@ -0,0 +1,3 @@
945ab4b16d05aa084f71bf5da9a3f687e0ec8bbd libnmdc-r1
02a360e95480b97ddad83add5db48b2766339a99 nmdc-log-service-1.0.0
137c1b65039e03c80379826a6efdfd808f6fbc8f libnmdc-r2

View File

@@ -0,0 +1,20 @@
An NMDC client protocol library for Golang.
- Channel-based API.
- Includes a sample logging client.
- This code hosting site isn't (yet) compatible with `go get`.
Written in golang
Tags: nmdc
=CHANGELOG=
2016-04-02 r2
- Enhancement: Support NMDC-over-TLS (NMDCS)
- Fix an issue recieving private messages
- Fix an issue with calling `panic()` if connection failed
- Fix an issue parsing URIs without a specified port
- Move sample content into directory with excluded build
2016-02-12 r1
- Initial public release

View File

@@ -2,17 +2,48 @@
package libnmdc
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/url"
"regexp"
"strings"
"time"
)
type ConnectionState int
type HubAddress string
type HubEventType int
type HubAddress string
func (this *HubAddress) parse() url.URL {
parsed, err := url.Parse(strings.ToLower(string(*this)))
if err != nil || len(parsed.Host) == 0 {
parsed = &url.URL{
Scheme: "nmdc",
Host: string(*this),
}
}
// Add default port if not specified
if !strings.ContainsRune(parsed.Host, ':') {
parsed.Host = parsed.Host + ":411"
}
return *parsed
}
func (this *HubAddress) IsSecure() bool {
parsed := this.parse()
return parsed.Scheme == "nmdcs" || parsed.Scheme == "dchubs"
}
func (this *HubAddress) GetHostOnly() string {
return this.parse().Host
}
const (
CONNECTIONSTATE_DISCONNECTED = 1
CONNECTIONSTATE_CONNECTING = 2 // Handshake in progress
@@ -35,15 +66,17 @@ const (
var rx_protocolMessage *regexp.Regexp
var rx_publicChat *regexp.Regexp
var rx_incomingTo *regexp.Regexp
var ErrNotConnected error = errors.New("Not connected")
func init() {
rx_protocolMessage = regexp.MustCompile("(?ms)^[^|]*|")
rx_publicChat = regexp.MustCompile("(?ms)^<([^>]*)> (.*)$")
rx_incomingTo = regexp.MustCompile("(?ms)^([^ ]+) From: ([^ ]+) $<([^>]*)> (.*)$")
rx_incomingTo = regexp.MustCompile("(?ms)^([^ ]+) From: ([^ ]+) \\$<([^>]*)> (.*)")
}
type HubConnectionOptions struct {
Address HubAddress
SkipVerifyTLS bool // using a negative verb, because bools default to false
Self UserInfo
NickPassword string
NumEventsToBuffer uint
@@ -62,7 +95,8 @@ type HubConnection struct {
OnEvent chan HubEvent
// Private state
conn net.Conn
conn net.Conn // this is an interface
connValid bool
sentOurHello bool
}
@@ -129,8 +163,12 @@ func (this *HubConnection) userJoined_Full(uinf *UserInfo) {
// Note that protocol messages are transmitted on the caller thread, not from
// any internal libnmdc thread.
func (this *HubConnection) SayRaw(protocolCommand string) error {
_, err := this.conn.Write([]byte(protocolCommand))
return err
if this.connValid {
_, err := this.conn.Write([]byte(protocolCommand))
return err
} else {
return ErrNotConnected
}
}
func parseLock(lock []byte) string {
@@ -243,15 +281,19 @@ func (this *HubConnection) processProtocolMessage(message string) {
}
}
case "$To":
case "$To:":
valid := false
if rx_incomingTo.MatchString(commandParts[1]) {
txparts := rx_incomingTo.FindStringSubmatch(commandParts[1])
if txparts[0] == this.Hco.Self.Nick && txparts[1] == txparts[2] {
this.OnEvent <- HubEvent{EventType: EVENT_PRIVATE, Nick: txparts[1], Message: txparts[3]}
break
if txparts[1] == this.Hco.Self.Nick && txparts[2] == txparts[3] {
this.OnEvent <- HubEvent{EventType: EVENT_PRIVATE, Nick: txparts[2], Message: txparts[4]}
valid = true
}
}
this.OnEvent <- HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Malformed private message '" + commandParts[1] + "'"}
if !valid {
this.OnEvent <- HubEvent{EventType: EVENT_DEBUG_MESSAGE, Message: "Malformed private message '" + commandParts[1] + "'"}
}
case "$UserIP":
// Final message in PtokaX connection handshake - trigger connection callback.
@@ -283,28 +325,42 @@ func (this *HubConnection) processProtocolMessage(message string) {
func (this *HubConnection) worker() {
var fullBuffer string
var err error = nil
var nbytes int = 0
for {
// If we're not connected, attempt reconnect
if this.conn == nil {
tmp, err := net.Dial("tcp", string(this.Hco.Address))
this.conn = tmp
if err == nil {
if this.Hco.Address.IsSecure() {
this.conn, err = tls.Dial("tcp", this.Hco.Address.GetHostOnly(), &tls.Config{
InsecureSkipVerify: this.Hco.SkipVerifyTLS,
})
} else {
this.conn, err = net.Dial("tcp", this.Hco.Address.GetHostOnly())
}
if err != nil {
this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_CONNECTING}
this.connValid = false
} else {
this.connValid = true
}
}
// Read from socket into our local buffer (blocking)
readBuff := make([]byte, 4096)
nbytes, err := this.conn.Read(readBuff)
if nbytes > 0 {
fullBuffer += string(readBuff[0:nbytes])
if this.connValid {
readBuff := make([]byte, 4096)
nbytes, err = this.conn.Read(readBuff)
if nbytes > 0 {
fullBuffer += string(readBuff[0:nbytes])
}
}
// Maybe we disconnected
if err != nil {
this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED}
this.OnEvent <- HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()}
this.conn = nil
time.Sleep(30 * time.Second) // Wait before reconnect
continue

View File

@@ -1,4 +1,8 @@
// nmdc project main.go
// +build ignore
// This sample file demonstrates use of the libnmdc.go library. It's excluded
// when building the library package, but you can run it via `go run libnmdc_sample.go`.
package main
import (
@@ -9,7 +13,7 @@ import (
func main() {
opts := libnmdc.HubConnectionOptions{
Address: "127.0.0.1:411",
Address: "127.0.0.1",
Self: libnmdc.UserInfo{Nick: "slowpoke9"},
}
hub := opts.Connect()
@@ -18,7 +22,7 @@ func main() {
event := <-hub.OnEvent
switch event.EventType {
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
fmt.Printf("Connection -- %s\n", event.StateChange.Format())
fmt.Printf("Connection -- %s (%s)\n", event.StateChange.Format(), event.Message)
case libnmdc.EVENT_PUBLIC:
fmt.Printf("Message from '%s': '%s'\n", event.Nick, event.Message)

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

@@ -0,0 +1,49 @@
#!/bin/bash
set -eu
export GOPATH=$(
cd ../../
cygpath -w "$(pwd)"
)
main() {
local version=""
read -p "Enter version string (blank for timestamp)> " version
if [[ $version == "" ]] ; then
version=$(date +%s)
fi
echo "Using '${version}' as the version."
if [[ -f nmdc-log-service.exe ]] ; then
rm ./nmdc-log-service.exe
fi
if [[ -f nmdc-log-service ]] ; then
rm ./nmdc-log-service
fi
echo "Building win64..."
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
rm ./nmdc-log-service.exe
echo "Building win32..."
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
rm ./nmdc-log-service.exe
echo "Building linux64..."
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
rm ./nmdc-log-service
echo "Building linux32..."
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
rm ./nmdc-log-service
echo "Build complete."
}
main "$@"

View File

@@ -0,0 +1,149 @@
package main
import (
"flag"
"fmt"
"libnmdc"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
var BaseDir string = "."
var PMResponse string = ""
var LogConnectionState bool
var DebugMode bool
var VerifyTLS bool
var CharacterMatcher *regexp.Regexp
func init() {
CharacterMatcher = regexp.MustCompile("[^a-zA-Z0-9]")
}
func GetDirectoryNameForHub(hub string) string {
return filepath.Join(BaseDir, CharacterMatcher.ReplaceAllString(hub, "_"))
}
func LogMessage(hub, message string) {
instant := time.Now() // using system timezone
// Log file path
monthStamp := instant.Format("2006-01")
logFilePath := filepath.Join(GetDirectoryNameForHub(hub), monthStamp+".log")
// Produce a timestamp
timeStamp := instant.Format("2006-01-02 15:04:05")
fh, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't open file '%s': %s\n", logFilePath, err.Error())
return
}
defer fh.Close()
_, err = fh.WriteString("[" + timeStamp + "] " + message + "\n")
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing to file '%s': %s\n", logFilePath, err.Error())
return
}
}
func HubWorker(addr, nick, password string) {
opts := libnmdc.HubConnectionOptions{
Address: libnmdc.HubAddress(addr),
SkipVerifyTLS: !VerifyTLS,
Self: libnmdc.UserInfo{Nick: nick},
NickPassword: password,
}
hub := opts.Connect()
for {
event := <-hub.OnEvent
if DebugMode {
fmt.Printf("DEBUG: %s %v\n", addr, event)
}
switch event.EventType {
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
if LogConnectionState {
if len(event.Message) > 0 {
LogMessage(addr, "* "+event.StateChange.Format()+" ("+event.Message+")")
} else {
LogMessage(addr, "* "+event.StateChange.Format())
}
}
case libnmdc.EVENT_PUBLIC:
LogMessage(addr, "<"+event.Nick+"> "+event.Message)
case libnmdc.EVENT_PRIVATE:
fmt.Printf("Got PM %v\n", event)
hub.SayPrivate(event.Nick, PMResponse)
case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB:
if strings.HasPrefix(event.Message, "* ") {
LogMessage(addr, event.Message)
} else {
LogMessage(addr, "* "+event.Message)
}
}
}
}
func main() {
// Parse arguments
hubs := flag.String("Server", "", "Addresses to connect to (comma-separated)")
nick := flag.String("Nick", "nmdc-log-service", "Nick")
password := flag.String("Password", "", "Registered nick password")
flag.StringVar(&BaseDir, "Dir", ".", "Output directory")
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.BoolVar(&DebugMode, "Debug", false, "Print additional information on stdout")
flag.BoolVar(&VerifyTLS, "VerifyTLS", true, "Verify TLS certificates")
flag.Parse()
// Assert dir exists
dinfo, err := os.Stat(BaseDir)
if err != nil {
fmt.Fprintf(os.Stderr, "FATAL: %s", err.Error())
os.Exit(1)
}
if !dinfo.IsDir() {
fmt.Fprintf(os.Stderr, "FATAL: Path '%s' is not a directory\n", BaseDir)
}
// Launch loggers
all_hubs := strings.Split(*hubs, ",")
launch_ct := 0
for _, hubaddr := range all_hubs {
if len(hubaddr) == 0 {
continue
}
// Assert target directory exists
os.MkdirAll(GetDirectoryNameForHub(hubaddr), 0755)
// Launch logger
go HubWorker(hubaddr, *nick, *password)
launch_ct++
}
if launch_ct == 0 {
fmt.Fprintln(os.Stderr, "FATAL: No hubs specified")
os.Exit(1)
}
// Wait forever
var forever chan bool = nil
select {
case <-forever:
}
}