Compare commits
21 Commits
nmdc-log-s
...
v0.9.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 7392cbbea5 | |||
| 265c0a43ce | |||
| 417deff347 | |||
| a996f1668c | |||
| 9b290ebb96 | |||
| 756b347592 | |||
| d4e442bd84 | |||
| bbdc18698d | |||
| b049acad90 | |||
| df0211c67d | |||
| 861090b0e9 | |||
| cc15a571a8 | |||
| 3c4972d930 | |||
| 43db3a0d64 | |||
| 8e4a45a2b9 | |||
| f350afa0fe | |||
| dea4f996f9 | |||
| 0fee719b90 | |||
| 15455ff031 | |||
| a287b3adcc | |||
| d6d9a1711c |
4
.hgtags
4
.hgtags
@@ -7,3 +7,7 @@ fca41372e400853775b02e951f9db91d87f41adb nmdc-log-service-1.0.1
|
||||
da9f123633f9c28be6435ed7898139665d4c39d9 nmdc-log-service-1.0.2
|
||||
75a78f6a78f249a2cd8aa3d29f7e5e6319b4e03b libnmdc-r5
|
||||
4116422bb10229d887f9296970a166fa1ef8c5fd nmdc-log-service-1.0.3
|
||||
cb86f3a40115cc46f450c0c83fd9b9d3b740e820 nmdc-log-service-1.0.4
|
||||
cb86f3a40115cc46f450c0c83fd9b9d3b740e820 libnmdc-r6
|
||||
71343a2c641a438206d30ea7e75dc89a11dbef00 libnmdc-r7
|
||||
b0e57a5fcffdf4102d669db51a3648ddf66a0792 libnmdc-r8
|
||||
|
||||
40
ConnectionState.go
Normal file
40
ConnectionState.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
type ConnectionState int
|
||||
|
||||
const (
|
||||
CONNECTIONSTATE_DISCONNECTED = 1
|
||||
CONNECTIONSTATE_CONNECTING = 2 // Handshake in progress
|
||||
CONNECTIONSTATE_CONNECTED = 3
|
||||
)
|
||||
|
||||
func (cs ConnectionState) Format() string {
|
||||
switch cs {
|
||||
case CONNECTIONSTATE_DISCONNECTED:
|
||||
return "Disconnected"
|
||||
case CONNECTIONSTATE_CONNECTING:
|
||||
return "Connecting"
|
||||
case CONNECTIONSTATE_CONNECTED:
|
||||
return "Connected"
|
||||
default:
|
||||
return "?"
|
||||
}
|
||||
}
|
||||
|
||||
func CheckIsNetTimeout(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
switch err.(type) {
|
||||
case net.Error:
|
||||
return err.(net.Error).Timeout()
|
||||
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
35
HubAddress.go
Normal file
35
HubAddress.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
@@ -1,147 +1,39 @@
|
||||
// libnmdc project libnmdc.go
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_CLIENT_TAG string = "libnmdc.go"
|
||||
DEFAULT_HUB_NAME string = "(unknown)"
|
||||
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
|
||||
)
|
||||
|
||||
type ConnectionState int
|
||||
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
|
||||
CONNECTIONSTATE_CONNECTED = 3
|
||||
)
|
||||
|
||||
const (
|
||||
EVENT_PUBLIC = 1
|
||||
EVENT_PRIVATE = 2
|
||||
EVENT_SYSTEM_MESSAGE_FROM_HUB = 3
|
||||
EVENT_SYSTEM_MESSAGE_FROM_CONN = 4
|
||||
EVENT_USER_JOINED = 5
|
||||
EVENT_USER_PART = 6
|
||||
EVENT_USER_UPDATED_INFO = 7
|
||||
EVENT_CONNECTION_STATE_CHANGED = 8
|
||||
EVENT_HUBNAME_CHANGED = 9
|
||||
EVENT_DEBUG_MESSAGE = 10
|
||||
)
|
||||
|
||||
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: ([^ ]+) \\$<([^>]*)> (.*)")
|
||||
}
|
||||
|
||||
type HubConnectionOptions struct {
|
||||
Address HubAddress
|
||||
SkipVerifyTLS bool // using a negative verb, because bools default to false
|
||||
Self UserInfo
|
||||
NickPassword string
|
||||
|
||||
// Returning messages in async mode
|
||||
NumEventsToBuffer uint
|
||||
|
||||
// Returning messages in sync mode
|
||||
OnEventSync func(HubEvent)
|
||||
}
|
||||
|
||||
type HubConnection struct {
|
||||
// Supplied parameters
|
||||
Hco *HubConnectionOptions
|
||||
|
||||
// Current remote status
|
||||
HubName string
|
||||
State ConnectionState
|
||||
Users map[string]UserInfo
|
||||
HubName string
|
||||
State ConnectionState
|
||||
users map[string]UserInfo
|
||||
userLock sync.RWMutex
|
||||
|
||||
// Streamed events
|
||||
processEvent func(HubEvent)
|
||||
OnEvent chan HubEvent
|
||||
|
||||
// Private state
|
||||
conn net.Conn // this is an interface
|
||||
connValid bool
|
||||
sentOurHello bool
|
||||
conn net.Conn // this is an interface
|
||||
connValid bool
|
||||
sentOurHello bool
|
||||
autoReconnect bool
|
||||
}
|
||||
|
||||
type HubEvent struct {
|
||||
EventType HubEventType
|
||||
Nick string
|
||||
Message string
|
||||
StateChange ConnectionState
|
||||
}
|
||||
func (this *HubConnection) Users(cb func(*map[string]UserInfo) error) error {
|
||||
this.userLock.Lock()
|
||||
defer this.userLock.Unlock()
|
||||
|
||||
func (cs ConnectionState) Format() string {
|
||||
switch cs {
|
||||
case CONNECTIONSTATE_DISCONNECTED:
|
||||
return "Disconnected"
|
||||
case CONNECTIONSTATE_CONNECTING:
|
||||
return "Connecting"
|
||||
case CONNECTIONSTATE_CONNECTED:
|
||||
return "Connected"
|
||||
default:
|
||||
return "?"
|
||||
}
|
||||
}
|
||||
|
||||
func NMDCUnescape(encoded string) string {
|
||||
v1 := strings.Replace(encoded, "$", "$", -1)
|
||||
v2 := strings.Replace(v1, "|", "|", -1)
|
||||
return strings.Replace(v2, "&", "&", -1)
|
||||
}
|
||||
|
||||
func NMDCEscape(plaintext string) string {
|
||||
v1 := strings.Replace(plaintext, "&", "&", -1)
|
||||
v2 := strings.Replace(v1, "|", "|", -1)
|
||||
return strings.Replace(v2, "$", "$", -1)
|
||||
return cb(&this.users)
|
||||
}
|
||||
|
||||
func (this *HubConnection) SayPublic(message string) {
|
||||
@@ -156,18 +48,41 @@ func (this *HubConnection) SayInfo() {
|
||||
this.SayRaw(this.Hco.Self.toMyINFO() + "|")
|
||||
}
|
||||
|
||||
func (this *HubConnection) UserExists(nick string) bool {
|
||||
this.userLock.RLock()
|
||||
defer this.userLock.RUnlock()
|
||||
|
||||
_, already_existed := this.users[nick]
|
||||
return already_existed
|
||||
}
|
||||
|
||||
func (this *HubConnection) UserCount() int {
|
||||
this.userLock.RLock()
|
||||
defer this.userLock.RUnlock()
|
||||
|
||||
return len(this.users)
|
||||
}
|
||||
|
||||
func (this *HubConnection) userJoined_NameOnly(nick string) {
|
||||
_, already_existed := this.Users[nick]
|
||||
if !already_existed {
|
||||
this.Users[nick] = *NewUserInfo(nick)
|
||||
if !this.UserExists(nick) {
|
||||
this.userLock.Lock()
|
||||
defer this.userLock.Unlock()
|
||||
|
||||
this.users[nick] = *NewUserInfo(nick)
|
||||
this.processEvent(HubEvent{EventType: EVENT_USER_JOINED, Nick: nick})
|
||||
}
|
||||
}
|
||||
|
||||
func (this *HubConnection) userJoined_Full(uinf *UserInfo) {
|
||||
_, already_existed := this.Users[uinf.Nick]
|
||||
if !already_existed {
|
||||
this.Users[uinf.Nick] = *uinf
|
||||
// n.b. also called when we get a replacement MyINFO for someone
|
||||
this.userLock.Lock()
|
||||
defer this.userLock.Unlock()
|
||||
|
||||
_, userExisted := this.users[uinf.Nick] // don't use UserExists as it would deadlock the mutex
|
||||
|
||||
this.users[uinf.Nick] = *uinf
|
||||
|
||||
if !userExisted {
|
||||
this.processEvent(HubEvent{EventType: EVENT_USER_JOINED, Nick: uinf.Nick})
|
||||
}
|
||||
}
|
||||
@@ -183,28 +98,6 @@ func (this *HubConnection) SayRaw(protocolCommand string) error {
|
||||
}
|
||||
}
|
||||
|
||||
func parseLock(lock []byte) string {
|
||||
|
||||
nibble_swap := func(b byte) byte {
|
||||
return ((b << 4) & 0xF0) | ((b >> 4) & 0x0F)
|
||||
}
|
||||
|
||||
chr := func(b byte) string {
|
||||
if b == 0 || b == 5 || b == 36 || b == 96 || b == 124 || b == 126 {
|
||||
return fmt.Sprintf("/%%DCN%04d%%/", b)
|
||||
} else {
|
||||
return string(b)
|
||||
}
|
||||
}
|
||||
|
||||
key := chr(nibble_swap(lock[0] ^ lock[len(lock)-2] ^ lock[len(lock)-3] ^ 5))
|
||||
for i := 1; i < len(lock); i += 1 {
|
||||
key += chr(nibble_swap(lock[i] ^ lock[i-1]))
|
||||
}
|
||||
|
||||
return key
|
||||
}
|
||||
|
||||
func (this *HubConnection) processProtocolMessage(message string) {
|
||||
|
||||
// Zero-length protocol message
|
||||
@@ -236,7 +129,7 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
|
||||
case "$Lock":
|
||||
this.SayRaw("$Supports NoGetINFO UserCommand UserIP2|" +
|
||||
"$Key " + parseLock([]byte(commandParts[1])) + "|" +
|
||||
"$Key " + NMDCUnlock([]byte(commandParts[1])) + "|" +
|
||||
"$ValidateNick " + NMDCEscape(this.Hco.Self.Nick) + "|")
|
||||
this.sentOurHello = false
|
||||
|
||||
@@ -273,7 +166,12 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
this.SayRaw("$MyPass " + NMDCEscape(this.Hco.NickPassword) + "|")
|
||||
|
||||
case "$Quit":
|
||||
delete(this.Users, commandParts[1])
|
||||
func() {
|
||||
this.userLock.Lock()
|
||||
defer this.userLock.Unlock()
|
||||
|
||||
delete(this.users, commandParts[1])
|
||||
}()
|
||||
this.processEvent(HubEvent{EventType: EVENT_USER_PART, Nick: commandParts[1]})
|
||||
|
||||
case "$MyINFO":
|
||||
@@ -293,6 +191,33 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
}
|
||||
}
|
||||
|
||||
case "$OpList":
|
||||
oplist := strings.Split(commandParts[1], "$$")
|
||||
opmap := map[string]struct{}{}
|
||||
|
||||
// Organise/sort the list, and ensure we're not meeting an operator for
|
||||
// the first time
|
||||
for _, nick := range oplist {
|
||||
if len(nick) > 0 {
|
||||
opmap[nick] = struct{}{}
|
||||
this.userJoined_NameOnly(nick) // assert existence; noop otherwise
|
||||
}
|
||||
}
|
||||
|
||||
// Mark all mentioned nicks as being operators, and all unmentioned nicks
|
||||
// as being /not/ an operator. (second pass minimises RW mutex use)
|
||||
func() {
|
||||
this.userLock.Lock()
|
||||
defer this.userLock.Unlock()
|
||||
|
||||
for nick, userinfo := range this.users {
|
||||
_, isop := opmap[nick]
|
||||
|
||||
userinfo.IsOperator = isop
|
||||
this.users[nick] = userinfo
|
||||
}
|
||||
}()
|
||||
|
||||
case "$To:":
|
||||
valid := false
|
||||
if rx_incomingTo.MatchString(commandParts[1]) {
|
||||
@@ -322,8 +247,6 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
// IGNORABLE COMMANDS
|
||||
case "$Supports":
|
||||
case "$UserCommand": // TODO $UserCommand 1 1 Group chat\New group chat$<%[mynick]> !groupchat_new||
|
||||
case "$UserList":
|
||||
case "$OpList":
|
||||
case "$HubTopic":
|
||||
case "$Search":
|
||||
case "$ConnectToMe":
|
||||
@@ -334,18 +257,12 @@ func (this *HubConnection) processProtocolMessage(message string) {
|
||||
}
|
||||
}
|
||||
|
||||
func CheckIsNetTimeout(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
switch err.(type) {
|
||||
case net.Error:
|
||||
return err.(net.Error).Timeout()
|
||||
|
||||
default:
|
||||
return false
|
||||
func (this *HubConnection) Disconnect() {
|
||||
this.autoReconnect = false
|
||||
if this.conn != nil {
|
||||
this.conn.Close()
|
||||
}
|
||||
// A CONNECTIONSTATE_DISCONNECTED message will be emitted by the worker.
|
||||
}
|
||||
|
||||
func (this *HubConnection) worker() {
|
||||
@@ -422,50 +339,14 @@ func (this *HubConnection) worker() {
|
||||
this.connValid = false
|
||||
this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()})
|
||||
|
||||
time.Sleep(30 * time.Second) // Wait before reconnect
|
||||
continue
|
||||
if this.autoReconnect {
|
||||
time.Sleep(AUTO_RECONNECT_AFTER) // Wait before reconnect
|
||||
continue
|
||||
} else {
|
||||
return // leave the worker for good
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (this *HubConnectionOptions) prepareConnection() *HubConnection {
|
||||
if this.Self.ClientTag == "" {
|
||||
this.Self.ClientTag = DEFAULT_CLIENT_TAG
|
||||
}
|
||||
|
||||
hc := HubConnection{
|
||||
Hco: this,
|
||||
HubName: DEFAULT_HUB_NAME,
|
||||
State: CONNECTIONSTATE_DISCONNECTED,
|
||||
Users: make(map[string]UserInfo),
|
||||
}
|
||||
|
||||
return &hc
|
||||
}
|
||||
|
||||
// Connects to an NMDC server, and spawns a background goroutine to handle
|
||||
// protocol messages. Client code should select on all the interface channels.
|
||||
func (this *HubConnectionOptions) Connect() *HubConnection {
|
||||
|
||||
if this.NumEventsToBuffer < 1 {
|
||||
this.NumEventsToBuffer = 1
|
||||
}
|
||||
|
||||
hc := this.prepareConnection()
|
||||
hc.OnEvent = make(chan HubEvent, this.NumEventsToBuffer)
|
||||
hc.processEvent = func(ev HubEvent) {
|
||||
hc.OnEvent <- ev
|
||||
}
|
||||
|
||||
go hc.worker()
|
||||
return hc
|
||||
}
|
||||
|
||||
// Connects to an NMDC server, and blocks forever to handle protocol messages.
|
||||
// Client code should supply an event handling function.
|
||||
func (this *HubConnectionOptions) ConnectSync() {
|
||||
hc := this.prepareConnection()
|
||||
hc.worker()
|
||||
}
|
||||
58
HubConnectionOptions.go
Normal file
58
HubConnectionOptions.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package libnmdc
|
||||
|
||||
type HubConnectionOptions struct {
|
||||
Address HubAddress
|
||||
SkipVerifyTLS bool // using a negative verb, because bools default to false
|
||||
SkipAutoReconnect bool // as above
|
||||
Self UserInfo
|
||||
NickPassword string
|
||||
|
||||
// Returning messages in async mode
|
||||
NumEventsToBuffer uint
|
||||
|
||||
// Returning messages in sync mode
|
||||
OnEventSync func(HubEvent)
|
||||
}
|
||||
|
||||
func (this *HubConnectionOptions) prepareConnection() *HubConnection {
|
||||
if this.Self.ClientTag == "" {
|
||||
this.Self.ClientTag = DEFAULT_CLIENT_TAG
|
||||
}
|
||||
|
||||
hc := HubConnection{
|
||||
Hco: this,
|
||||
HubName: DEFAULT_HUB_NAME,
|
||||
State: CONNECTIONSTATE_DISCONNECTED,
|
||||
users: make(map[string]UserInfo),
|
||||
|
||||
autoReconnect: !this.SkipAutoReconnect,
|
||||
}
|
||||
|
||||
return &hc
|
||||
}
|
||||
|
||||
// Connects to an NMDC server, and spawns a background goroutine to handle
|
||||
// protocol messages. Events will be sent by channel to the returned hc.OnEvent,
|
||||
// the client is responsible for selecting off this.
|
||||
func (this *HubConnectionOptions) Connect() *HubConnection {
|
||||
|
||||
if this.NumEventsToBuffer < 1 {
|
||||
this.NumEventsToBuffer = 1
|
||||
}
|
||||
|
||||
hc := this.prepareConnection()
|
||||
hc.OnEvent = make(chan HubEvent, this.NumEventsToBuffer)
|
||||
hc.processEvent = func(ev HubEvent) {
|
||||
hc.OnEvent <- ev
|
||||
}
|
||||
|
||||
go hc.worker()
|
||||
return hc
|
||||
}
|
||||
|
||||
// Connects to an NMDC server, and blocks forever to handle protocol messages.
|
||||
// Client code should supply an event handling function as hco.OnEventSync.
|
||||
func (this *HubConnectionOptions) ConnectSync() {
|
||||
hc := this.prepareConnection()
|
||||
hc.worker()
|
||||
}
|
||||
23
HubEvent.go
Normal file
23
HubEvent.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package libnmdc
|
||||
|
||||
const (
|
||||
EVENT_PUBLIC = 1
|
||||
EVENT_PRIVATE = 2
|
||||
EVENT_SYSTEM_MESSAGE_FROM_HUB = 3
|
||||
EVENT_SYSTEM_MESSAGE_FROM_CONN = 4
|
||||
EVENT_USER_JOINED = 5
|
||||
EVENT_USER_PART = 6
|
||||
EVENT_USER_UPDATED_INFO = 7
|
||||
EVENT_CONNECTION_STATE_CHANGED = 8
|
||||
EVENT_HUBNAME_CHANGED = 9
|
||||
EVENT_DEBUG_MESSAGE = 10
|
||||
)
|
||||
|
||||
type HubEventType int
|
||||
|
||||
type HubEvent struct {
|
||||
EventType HubEventType
|
||||
Nick string
|
||||
Message string
|
||||
StateChange ConnectionState
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UserFlag byte
|
||||
@@ -37,6 +38,7 @@ type UserInfo struct {
|
||||
Nick string
|
||||
Description string
|
||||
ClientTag string
|
||||
ClientVersion string
|
||||
Email string
|
||||
ShareSize uint64
|
||||
ConnectionMode ConnectionMode
|
||||
@@ -46,6 +48,7 @@ type UserInfo struct {
|
||||
HubsUnregistered uint64
|
||||
HubsRegistered uint64
|
||||
HubsOperator uint64
|
||||
IsOperator bool
|
||||
}
|
||||
|
||||
var rx_myinfo *regexp.Regexp
|
||||
@@ -53,12 +56,19 @@ var rx_myinfo_notag *regexp.Regexp
|
||||
|
||||
func init() {
|
||||
// $ALL <nick> <description>$ $<connection><flag>$<e-mail>$<sharesize>$
|
||||
rx_myinfo = regexp.MustCompile("(?ms)^\\$ALL ([^ ]+) ([^<]*)<([^,]*),M:(.),H:([0-9]+)/([0-9]+)/([0-9]+),S:([0-9]+)>\\$.\\$(.+?)(.)\\$([^$]*)\\$([0-9]*)\\$$")
|
||||
rx_myinfo_notag = regexp.MustCompile("(?ms)^\\$ALL ([^ ]+) ([^$]*)\\$.\\$(.*)(.)\\$([^$]*)\\$([0-9]*)\\$$") // Fallback for no tag
|
||||
|
||||
HEAD := `(?ms)^\$ALL ([^ ]+) `
|
||||
FOOT := `\$.\$([^$]+)\$([^$]*)\$([0-9]*)\$$`
|
||||
|
||||
rx_myinfo = regexp.MustCompile(HEAD + `([^<]*)<(.+?) V:([^,]+),M:(.),H:([0-9]+)/([0-9]+)/([0-9]+),S:([0-9]+)>` + FOOT)
|
||||
rx_myinfo_notag = regexp.MustCompile(HEAD + `([^$]*)` + FOOT) // Fallback for no tag
|
||||
|
||||
/*
|
||||
sample := "$ALL Betty description<ApexDC++ V:1.4.3,M:P,H:9/0/2,S:1>$ $0.01\x01$xyz@xyz.com$53054999578$"
|
||||
sample = "$ALL ivysaur80 $P$10A$$0$"
|
||||
sample := ""
|
||||
// sample = "$ALL Betty description<ApexDC++ V:1.4.3,M:P,H:9/0/2,S:1>$ $0.01\x01$xyz@xyz.com$53054999578$"
|
||||
// sample = "$ALL ivysaur80 $P$10A$$0$"
|
||||
// sample = "$ALL SHINY_IVYSAUR <ncdc V:1.19.1-12-g5561,M:P,H:1/0/0,S:10>$ $0.005Q$$0$"
|
||||
// sample = "$ALL mappu desccccc<HexChat V:2.12.1,M:P,H:1/0/0,S:0>$ $p$$0$"
|
||||
|
||||
u := UserInfo{}
|
||||
err := u.fromMyINFO(sample)
|
||||
@@ -68,7 +78,7 @@ func init() {
|
||||
fmt.Printf("%+v\n", u)
|
||||
}
|
||||
|
||||
os.Exit(1)
|
||||
panic("")
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -90,19 +100,25 @@ func maybeParse(str string, dest *uint64, default_val uint64) {
|
||||
}
|
||||
|
||||
func (this *UserInfo) fromMyINFO(protomsg string) error {
|
||||
// Normal format (with tag in exact M/H/S order)
|
||||
|
||||
// Normal format (with tag in exact V/M/H/S order)
|
||||
matches := rx_myinfo.FindStringSubmatch(protomsg)
|
||||
if matches != nil {
|
||||
this.Nick = matches[1]
|
||||
this.Description = NMDCUnescape(matches[2])
|
||||
this.ClientTag = NMDCUnescape(matches[3])
|
||||
this.ClientVersion = matches[4]
|
||||
this.ConnectionMode = ConnectionMode(matches[4][0])
|
||||
maybeParse(matches[5], &this.HubsUnregistered, 0)
|
||||
maybeParse(matches[6], &this.HubsRegistered, 0)
|
||||
maybeParse(matches[7], &this.HubsOperator, 0)
|
||||
maybeParse(matches[8], &this.Slots, 0)
|
||||
this.Speed = matches[9]
|
||||
this.Flag = UserFlag(matches[10][0])
|
||||
maybeParse(matches[6], &this.HubsUnregistered, 0)
|
||||
maybeParse(matches[7], &this.HubsRegistered, 0)
|
||||
maybeParse(matches[8], &this.HubsOperator, 0)
|
||||
maybeParse(matches[9], &this.Slots, 0)
|
||||
if len(matches[10]) > 1 {
|
||||
this.Speed = matches[10][:len(matches[10])-2]
|
||||
} else {
|
||||
this.Speed = ""
|
||||
}
|
||||
this.Flag = UserFlag(matches[10][len(matches[10])-1])
|
||||
this.Email = NMDCUnescape(matches[11])
|
||||
maybeParse(matches[12], &this.ShareSize, 0)
|
||||
|
||||
@@ -115,19 +131,27 @@ func (this *UserInfo) fromMyINFO(protomsg string) error {
|
||||
this.Nick = matches[1]
|
||||
this.Description = NMDCUnescape(matches[2])
|
||||
this.ClientTag = ""
|
||||
this.ClientVersion = "0"
|
||||
this.ConnectionMode = CONNECTIONMODE_PASSIVE
|
||||
this.HubsUnregistered = 0
|
||||
this.HubsRegistered = 0
|
||||
this.HubsOperator = 0
|
||||
this.Slots = 0
|
||||
this.Speed = matches[3]
|
||||
this.Flag = UserFlag(matches[4][0])
|
||||
this.Email = NMDCUnescape(matches[5])
|
||||
maybeParse(matches[6], &this.ShareSize, 0)
|
||||
|
||||
if len(matches[3]) > 1 {
|
||||
this.Speed = matches[3][:len(matches[3])-2]
|
||||
} else {
|
||||
this.Speed = ""
|
||||
}
|
||||
this.Flag = UserFlag(matches[3][len(matches[3])-1])
|
||||
this.Email = NMDCUnescape(matches[4])
|
||||
maybeParse(matches[5], &this.ShareSize, 0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Printf("PARSE: malformed\n")
|
||||
|
||||
// Couldn't get anything out of it...
|
||||
return errors.New("Malformed MyINFO")
|
||||
}
|
||||
@@ -135,10 +159,11 @@ func (this *UserInfo) fromMyINFO(protomsg string) error {
|
||||
// Returns the MyINFO command, WITH leading $MyINFO, and WITHOUT trailing pipe
|
||||
func (this *UserInfo) toMyINFO() string {
|
||||
return fmt.Sprintf(
|
||||
"$MyINFO $ALL %s %s<%s,M:%c,H:%d/%d/%d,S:%d>$ $%s%c$%s$%d$",
|
||||
"$MyINFO $ALL %s %s<%s V:%s,M:%c,H:%d/%d/%d,S:%d>$ $%s%c$%s$%d$",
|
||||
this.Nick,
|
||||
this.Description,
|
||||
this.ClientTag,
|
||||
strings.Replace(this.ClientVersion, ",", "-", -1), // just in case
|
||||
this.ConnectionMode,
|
||||
this.HubsUnregistered,
|
||||
this.HubsRegistered,
|
||||
@@ -9,6 +9,21 @@ Tags: nmdc
|
||||
|
||||
=CHANGELOG=
|
||||
|
||||
2016-08-27 r9
|
||||
- Fix an issue with parsing MyINFO strings with zero-length speed descriptions
|
||||
- Fix an issue with not storing updated profile information
|
||||
|
||||
2016-05-10 r8
|
||||
- Enhancement: Separate `ClientTag` and `ClientVersion` in `UserInfo` structs
|
||||
|
||||
2016-05-08 r7
|
||||
- BREAKING: Remove direct access to `HubConnection.Users` map
|
||||
- Feature: Threadsafe user map accessor
|
||||
- Feature: Option to disable auto-reconnection
|
||||
- Feature: New `Disconnect()`, `UserCount()`, `UserExists()` functions
|
||||
- Enhancement: Support `$OpList`, add `IsOperator` member to `UserInfo` structs
|
||||
- Refactor into multiple files
|
||||
|
||||
2016-04-16 r6
|
||||
- Fix an issue with calling `panic()` on certain types of abnormal network failure
|
||||
|
||||
62
libnmdc.go
Normal file
62
libnmdc.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// libnmdc project libnmdc.go
|
||||
package libnmdc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_CLIENT_TAG string = "libnmdc.go"
|
||||
DEFAULT_HUB_NAME string = "(unknown)"
|
||||
SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second
|
||||
AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second
|
||||
)
|
||||
|
||||
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: ([^ ]+) \\$<([^>]*)> (.*)")
|
||||
}
|
||||
|
||||
func NMDCUnescape(encoded string) string {
|
||||
v1 := strings.Replace(encoded, "$", "$", -1)
|
||||
v2 := strings.Replace(v1, "|", "|", -1)
|
||||
return strings.Replace(v2, "&", "&", -1)
|
||||
}
|
||||
|
||||
func NMDCEscape(plaintext string) string {
|
||||
v1 := strings.Replace(plaintext, "&", "&", -1)
|
||||
v2 := strings.Replace(v1, "|", "|", -1)
|
||||
return strings.Replace(v2, "$", "$", -1)
|
||||
}
|
||||
|
||||
func NMDCUnlock(lock []byte) string {
|
||||
|
||||
nibble_swap := func(b byte) byte {
|
||||
return ((b << 4) & 0xF0) | ((b >> 4) & 0x0F)
|
||||
}
|
||||
|
||||
chr := func(b byte) string {
|
||||
if b == 0 || b == 5 || b == 36 || b == 96 || b == 124 || b == 126 {
|
||||
return fmt.Sprintf("/%%DCN%04d%%/", b)
|
||||
} else {
|
||||
return string(b)
|
||||
}
|
||||
}
|
||||
|
||||
key := chr(nibble_swap(lock[0] ^ lock[len(lock)-2] ^ lock[len(lock)-3] ^ 5))
|
||||
for i := 1; i < len(lock); i += 1 {
|
||||
key += chr(nibble_swap(lock[i] ^ lock[i-1]))
|
||||
}
|
||||
|
||||
return key
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
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)
|
||||
-VerifyTLS
|
||||
Verify TLS certificates (default true)
|
||||
`
|
||||
|
||||
=CHANGELOG=
|
||||
|
||||
2016-04-16 1.0.4
|
||||
- Enhancement: Upgrade `libnmdc` from `r5` to `r6`
|
||||
- Include a sample systemd unit script in source archive
|
||||
- Fix an issue showing zero connected hubs in user tag
|
||||
- Fix a cosmetic issue with logging repeated identical connection failure messages
|
||||
|
||||
2016-04-04 1.0.3
|
||||
- Enhancement: Upgrade `libnmdc` from `r4` to `r5`
|
||||
|
||||
2016-04-03 1.0.2
|
||||
- Enhancement: Upgrade `libnmdc` from `r3` to `r4`
|
||||
|
||||
2016-04-03 1.0.1
|
||||
- Enhancement: Add `-VerifyTLS` option
|
||||
- Enhancement: Upgrade `libnmdc` from `r2` to `r3`
|
||||
- Fix an issue writing log files on Linux
|
||||
- Fix a cosmetic issue with error message formatting
|
||||
|
||||
2016-04-02 1.0.0
|
||||
- Initial public release
|
||||
@@ -1,59 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -eu
|
||||
|
||||
export GOPATH=$(
|
||||
cd ../../
|
||||
cygpath -w "$(pwd)"
|
||||
)
|
||||
|
||||
sanitise() {
|
||||
local tmp=$(mktemp)
|
||||
cat "$1" | perl -pe 's~C:.Users.......Documents.DEV.~C:/xxxxxxxxxxxxxxxxxxxxxxxxx/~g' > "$tmp"
|
||||
mv "$tmp" "$1"
|
||||
}
|
||||
|
||||
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
|
||||
sanitise 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
|
||||
sanitise 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
|
||||
sanitise 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
|
||||
sanitise 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 "$@"
|
||||
@@ -1,14 +0,0 @@
|
||||
[Unit]
|
||||
Description=NMDC Log Service
|
||||
|
||||
[Service]
|
||||
Environment=TZ=Etc/UTC
|
||||
Type=simple
|
||||
ExecStart=/home/hublog/nmdc-log-service -Dir /home/hublog/logs/ -Server ... -Nick ... -Password '...' -VerifyTLS=false
|
||||
Restart=always
|
||||
RestartSec=30s
|
||||
User=hublog
|
||||
Group=nogroup
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -1,163 +0,0 @@
|
||||
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|os.O_WRONLY, 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
|
||||
}
|
||||
}
|
||||
|
||||
type HubWorker struct {
|
||||
LastConnectionStateMessage string
|
||||
}
|
||||
|
||||
func (this *HubWorker) MainLoop(addr, nick, password string) {
|
||||
|
||||
opts := libnmdc.HubConnectionOptions{
|
||||
Address: libnmdc.HubAddress(addr),
|
||||
SkipVerifyTLS: !VerifyTLS,
|
||||
Self: *libnmdc.NewUserInfo(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 {
|
||||
str := "* " + event.StateChange.Format()
|
||||
if len(event.Message) > 0 {
|
||||
str += " (" + event.Message + ")"
|
||||
}
|
||||
|
||||
// Prevent logging the same message repeatedly
|
||||
if str != this.LastConnectionStateMessage {
|
||||
LogMessage(addr, str)
|
||||
}
|
||||
this.LastConnectionStateMessage = str
|
||||
}
|
||||
|
||||
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\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
|
||||
for _, hubaddr := range all_hubs {
|
||||
if len(hubaddr) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Assert target directory exists
|
||||
os.MkdirAll(GetDirectoryNameForHub(hubaddr), 0755)
|
||||
|
||||
// Launch logger
|
||||
hw := HubWorker{}
|
||||
go hw.MainLoop(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:
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user