14 Commits

8 changed files with 127 additions and 22 deletions

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

@@ -1,7 +0,0 @@
An NMDC client protocol library for Golang.
- Includes a sample logging client.
- This code hosting site isn't yet compatible with `go get`.
Written in golang
Tags: nmdc

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

@@ -3,6 +3,7 @@ package libnmdc
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/url"
@@ -65,6 +66,7 @@ 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)^[^|]*|")
@@ -74,6 +76,7 @@ func init() {
type HubConnectionOptions struct {
Address HubAddress
SkipVerifyTLS bool // using a negative verb, because bools default to false
Self UserInfo
NickPassword string
NumEventsToBuffer uint
@@ -92,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
}
@@ -159,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 {
@@ -326,18 +334,23 @@ func (this *HubConnection) worker() {
if this.conn == nil {
if this.Hco.Address.IsSecure() {
this.conn, err = tls.Dial("tcp", this.Hco.Address.GetHostOnly(), nil)
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 {
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)
if this.conn != nil {
if this.connValid {
readBuff := make([]byte, 4096)
nbytes, err = this.conn.Read(readBuff)
if nbytes > 0 {

View File

@@ -0,0 +1,38 @@
// +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 (
"fmt"
"libnmdc"
)
func main() {
opts := libnmdc.HubConnectionOptions{
Address: "127.0.0.1",
Self: libnmdc.UserInfo{Nick: "slowpoke9"},
}
hub := opts.Connect()
for {
event := <-hub.OnEvent
switch event.EventType {
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
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)
if event.Message == "how are you" {
hub.SayPublic("good thanks!")
}
default:
fmt.Printf("%+v\n", event)
}
}
}

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
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
rm ./nmdc-log-service.exe
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
rm ./nmdc-log-service.exe
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
rm ./nmdc-log-service
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
rm ./nmdc-log-service

View File

@@ -15,6 +15,7 @@ var BaseDir string = "."
var PMResponse string = ""
var LogConnectionState bool
var DebugMode bool
var VerifyTLS bool
var CharacterMatcher *regexp.Regexp
func init() {
@@ -36,7 +37,7 @@ func LogMessage(hub, message string) {
// Produce a timestamp
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 {
fmt.Fprintf(os.Stderr, "Couldn't open file '%s': %s\n", logFilePath, err.Error())
return
@@ -53,9 +54,10 @@ func LogMessage(hub, message string) {
func HubWorker(addr, nick, password string) {
opts := libnmdc.HubConnectionOptions{
Address: libnmdc.HubAddress(addr),
Self: libnmdc.UserInfo{Nick: nick},
NickPassword: password,
Address: libnmdc.HubAddress(addr),
SkipVerifyTLS: !VerifyTLS,
Self: libnmdc.UserInfo{Nick: nick},
NickPassword: password,
}
hub := opts.Connect()
@@ -104,18 +106,23 @@ func main() {
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())
fmt.Fprintf(os.Stderr, "FATAL: %s\n", err.Error())
os.Exit(1)
}
if !dinfo.IsDir() {
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
all_hubs := strings.Split(*hubs, ",")
launch_ct := 0