2018-07-22 01:52:49 +00:00
|
|
|
package main
|
|
|
|
|
2018-08-02 01:13:10 +00:00
|
|
|
// TODO learn more about chan chan's
|
2018-07-30 06:12:09 +00:00
|
|
|
// http://marcio.io/2015/07/handling-1-million-requests-per-minute-with-golang/
|
2018-07-29 06:45:12 +00:00
|
|
|
|
2018-07-22 01:52:49 +00:00
|
|
|
import (
|
2018-07-31 05:51:46 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-07-31 07:44:45 +00:00
|
|
|
"github.com/emicklei/go-restful"
|
2018-07-31 05:51:46 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2018-07-22 01:52:49 +00:00
|
|
|
)
|
|
|
|
|
2018-07-30 06:12:09 +00:00
|
|
|
// I'm not sure how to pass nested structs, so I de-nested this.
|
2018-08-02 01:13:10 +00:00
|
|
|
// TODO Learn if passing nested structs is desirable?
|
2018-07-30 06:12:09 +00:00
|
|
|
type Conf struct {
|
2018-08-02 08:13:56 +00:00
|
|
|
Addr string `yaml:"addr,omitempty"`
|
|
|
|
Port uint `yaml:"port,omitempty"`
|
2018-07-31 07:44:45 +00:00
|
|
|
Mailer ConfMailer
|
|
|
|
RootPath string `yaml:"root_path,omitempty"`
|
2018-07-30 06:12:09 +00:00
|
|
|
}
|
2018-07-30 05:16:09 +00:00
|
|
|
type ConfMailer struct {
|
2018-07-31 05:51:46 +00:00
|
|
|
Url string `yaml:"url,omitempty"`
|
|
|
|
ApiKey string `yaml:"api_key,omitempty"`
|
|
|
|
From string `yaml:"from,omitempty"`
|
2018-07-30 05:16:09 +00:00
|
|
|
}
|
2018-07-29 22:58:15 +00:00
|
|
|
|
2018-07-30 23:02:21 +00:00
|
|
|
type tcpUser struct {
|
2018-07-31 05:51:46 +00:00
|
|
|
bufConn bufferedConn
|
|
|
|
userCount chan int
|
|
|
|
email string
|
2018-08-02 06:23:55 +00:00
|
|
|
newMsg chan string
|
2018-07-30 23:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 06:12:09 +00:00
|
|
|
// So we can peek at net.Conn, which we can't do natively
|
|
|
|
// https://stackoverflow.com/questions/51472020/how-to-get-the-size-of-available-tcp-data
|
2018-07-29 06:45:12 +00:00
|
|
|
type bufferedConn struct {
|
|
|
|
r *bufio.Reader
|
2018-08-02 01:13:10 +00:00
|
|
|
rout io.Reader // See https://github.com/polvi/sni/blob/master/sni.go#L135
|
2018-07-29 06:45:12 +00:00
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBufferedConn(c net.Conn) bufferedConn {
|
|
|
|
return bufferedConn{bufio.NewReader(c), nil, c}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b bufferedConn) Peek(n int) ([]byte, error) {
|
|
|
|
return b.r.Peek(n)
|
|
|
|
}
|
|
|
|
|
2018-07-31 05:51:46 +00:00
|
|
|
func (b bufferedConn) Buffered() int {
|
2018-07-29 06:45:12 +00:00
|
|
|
return b.r.Buffered()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b bufferedConn) Read(p []byte) (int, error) {
|
|
|
|
if b.rout != nil {
|
|
|
|
return b.rout.Read(p)
|
|
|
|
}
|
|
|
|
return b.r.Read(p)
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:12:09 +00:00
|
|
|
// Just making these all globals right now
|
|
|
|
// because... I can clean it up later
|
2018-07-22 03:58:05 +00:00
|
|
|
type myMsg struct {
|
2018-07-31 05:51:46 +00:00
|
|
|
sender net.Conn
|
2018-08-02 01:13:10 +00:00
|
|
|
Message string `json:"message"`
|
|
|
|
ReceivedAt time.Time `json:"received_at"`
|
|
|
|
Channel string `json:"channel"`
|
|
|
|
User string `json:"user"`
|
|
|
|
}
|
|
|
|
type JsonMsg struct {
|
2018-08-02 06:23:55 +00:00
|
|
|
Messages []*myMsg `json:"messages"`
|
2018-07-22 03:58:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 06:23:55 +00:00
|
|
|
type chatHist struct {
|
|
|
|
msgs []*myMsg
|
|
|
|
i int
|
|
|
|
c int
|
|
|
|
}
|
|
|
|
|
|
|
|
var myChatHist chatHist
|
2018-08-02 01:13:10 +00:00
|
|
|
var broadcastMsg chan myMsg
|
2018-07-31 05:51:46 +00:00
|
|
|
|
2018-08-02 07:09:34 +00:00
|
|
|
var virginConns chan net.Conn
|
2018-08-02 06:23:55 +00:00
|
|
|
var wantsServerHello chan bufferedConn
|
2018-08-02 07:34:00 +00:00
|
|
|
var authTelnet chan tcpUser
|
|
|
|
var cleanTelnet chan tcpUser
|
2018-08-02 06:23:55 +00:00
|
|
|
var gotClientHello chan bufferedConn
|
2018-08-02 07:09:34 +00:00
|
|
|
|
2018-08-02 08:13:56 +00:00
|
|
|
// HTTP
|
2018-08-02 06:23:55 +00:00
|
|
|
var demuxHttpClient chan bufferedConn
|
2018-08-01 02:35:40 +00:00
|
|
|
var newAuthReqs chan authReq
|
|
|
|
var valAuthReqs chan authReq
|
|
|
|
var delAuthReqs chan authReq
|
2018-07-22 03:58:05 +00:00
|
|
|
|
2018-07-22 01:52:49 +00:00
|
|
|
func usage() {
|
2018-07-31 05:51:46 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "\nusage: go run chatserver.go\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
fmt.Println()
|
2018-07-22 01:52:49 +00:00
|
|
|
|
2018-07-31 05:51:46 +00:00
|
|
|
os.Exit(1)
|
2018-07-22 01:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 05:16:09 +00:00
|
|
|
// https://blog.questionable.services/article/generating-secure-random-numbers-crypto-rand/
|
|
|
|
func genAuthCode() (string, error) {
|
2018-07-31 05:51:46 +00:00
|
|
|
n := 12
|
2018-07-30 05:16:09 +00:00
|
|
|
b := make([]byte, n)
|
|
|
|
_, err := rand.Read(b)
|
2018-07-31 05:51:46 +00:00
|
|
|
// Note that err == nil only if we read len(b) bytes.
|
2018-07-30 05:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return base64.URLEncoding.EncodeToString(b), nil
|
|
|
|
}
|
|
|
|
|
2018-08-02 06:23:55 +00:00
|
|
|
func muxTcp(conn bufferedConn) {
|
2018-07-31 05:51:46 +00:00
|
|
|
// Wish List for protocol detection
|
|
|
|
// * PROXY protocol (and loop)
|
|
|
|
// * HTTP CONNECT (proxy) (and loop)
|
|
|
|
// * tls (and loop) https://github.com/polvi/sni
|
|
|
|
// * http/ws
|
|
|
|
// * irc
|
|
|
|
// * fallback to telnet
|
|
|
|
|
|
|
|
// At this piont we've already at least one byte via Peek()
|
|
|
|
// so the first packet is available in the buffer
|
|
|
|
|
|
|
|
// Note: Realistically no tls/http/irc client is going to send so few bytes
|
|
|
|
// (and no router is going to chunk so small)
|
|
|
|
// that it cannot reasonably detect the protocol in the first packet
|
|
|
|
// However, typical MTU is 1,500 and HTTP can have a 2k URL
|
|
|
|
// so expecting to find the "HTTP/1.1" in the Peek is not always reasonable
|
|
|
|
n := conn.Buffered()
|
|
|
|
firstMsg, err := conn.Peek(n)
|
|
|
|
if nil != err {
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var protocol string
|
|
|
|
// between A and z
|
|
|
|
if firstMsg[0] >= 65 && firstMsg[0] <= 122 {
|
|
|
|
i := bytes.Index(firstMsg, []byte(" /"))
|
|
|
|
if -1 != i {
|
|
|
|
protocol = "HTTP"
|
|
|
|
// very likely HTTP
|
|
|
|
j := bytes.IndexAny(firstMsg, "\r\n")
|
|
|
|
if -1 != j {
|
|
|
|
k := bytes.Index(bytes.ToLower(firstMsg[:j]), []byte("HTTP/1"))
|
|
|
|
if -1 != k {
|
|
|
|
// positively HTTP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if 0x16 /*22*/ == firstMsg[0] {
|
|
|
|
// Because I don't always remember off the top of my head what the first byte is
|
|
|
|
// http://blog.fourthbit.com/2014/12/23/traffic-analysis-of-an-ssl-slash-tls-session
|
|
|
|
// https://tlseminar.github.io/first-few-milliseconds/
|
|
|
|
// TODO I want to learn about ALPN
|
|
|
|
protocol = "TLS"
|
|
|
|
}
|
|
|
|
|
|
|
|
if "" == protocol {
|
|
|
|
fmt.Fprintf(conn, "\n\nWelcome to Sample Chat! You're not an HTTP client, assuming Telnet.\nYou must authenticate via email to participate\n\nEmail: ")
|
2018-08-02 06:23:55 +00:00
|
|
|
wantsServerHello <- conn
|
2018-07-31 05:51:46 +00:00
|
|
|
return
|
|
|
|
} else if "HTTP" != protocol {
|
|
|
|
defer conn.Close()
|
|
|
|
fmt.Fprintf(conn, "\n\nNot yet supported. Try HTTP or Telnet\n\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-02 06:23:55 +00:00
|
|
|
demuxHttpClient <- conn
|
2018-07-22 01:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 05:16:09 +00:00
|
|
|
func handleConnection(netConn net.Conn) {
|
2018-07-31 05:51:46 +00:00
|
|
|
ts := time.Now()
|
|
|
|
fmt.Fprintf(os.Stdout, "[New Connection] (%s) welcome %s\n", ts, netConn.RemoteAddr().String())
|
2018-07-29 06:45:12 +00:00
|
|
|
|
2018-07-31 05:51:46 +00:00
|
|
|
m := sync.Mutex{}
|
|
|
|
virgin := true
|
2018-07-29 06:45:12 +00:00
|
|
|
|
2018-07-30 05:16:09 +00:00
|
|
|
bufConn := newBufferedConn(netConn)
|
2018-07-31 05:51:46 +00:00
|
|
|
go func() {
|
|
|
|
// Handle First Packet
|
|
|
|
_, err := bufConn.Peek(1)
|
|
|
|
//fmsg, err := bufConn.Peek(1)
|
|
|
|
if nil != err {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
//fmt.Fprintf(os.Stdout, "[First Byte] %s\n", fmsg)
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
if virgin {
|
|
|
|
virgin = false
|
2018-08-02 06:23:55 +00:00
|
|
|
gotClientHello <- bufConn
|
2018-07-31 05:51:46 +00:00
|
|
|
} else {
|
2018-08-02 06:23:55 +00:00
|
|
|
wantsServerHello <- bufConn
|
2018-07-31 05:51:46 +00:00
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(250 * 1000000)
|
|
|
|
// If we still haven't received data from the client
|
|
|
|
// assume that the client must be expecting a welcome from us
|
|
|
|
m.Lock()
|
|
|
|
if virgin {
|
|
|
|
virgin = false
|
|
|
|
// don't block for this
|
|
|
|
// let it be handled after the unlock
|
2018-08-02 01:13:10 +00:00
|
|
|
defer fmt.Fprintf(netConn,
|
|
|
|
"\n\nWelcome to Sample Chat! You appear to be using Telnet (http is also available on this port)."+
|
|
|
|
"\nYou must authenticate via email to participate\n\nEmail: ")
|
2018-07-31 05:51:46 +00:00
|
|
|
}
|
|
|
|
m.Unlock()
|
2018-07-29 06:45:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 05:16:09 +00:00
|
|
|
func sendAuthCode(cnf ConfMailer, to string) (string, error) {
|
2018-07-31 05:51:46 +00:00
|
|
|
code, err := genAuthCode()
|
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-07-30 05:16:09 +00:00
|
|
|
|
2018-07-31 05:51:46 +00:00
|
|
|
// TODO use go text templates with HTML escaping
|
|
|
|
text := "Your authorization code:\n\n" + code
|
|
|
|
html := "Your authorization code:<br><br>" + code
|
2018-07-30 05:16:09 +00:00
|
|
|
|
2018-07-31 05:51:46 +00:00
|
|
|
// https://stackoverflow.com/questions/24493116/how-to-send-a-post-request-in-go
|
|
|
|
// https://stackoverflow.com/questions/16673766/basic-http-auth-in-go
|
2018-07-30 05:16:09 +00:00
|
|
|
client := http.Client{}
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Add("from", cnf.From)
|
|
|
|
form.Add("to", to)
|
2018-07-31 05:51:46 +00:00
|
|
|
form.Add("subject", "Sample Chat Auth Code: "+code)
|
2018-07-30 05:16:09 +00:00
|
|
|
form.Add("text", text)
|
|
|
|
form.Add("html", html)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", cnf.Url, strings.NewReader(form.Encode()))
|
2018-07-31 05:51:46 +00:00
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-07-30 05:16:09 +00:00
|
|
|
//req.PostForm = form
|
|
|
|
req.Header.Add("User-Agent", "golang http.Client - Sample Chat App Authenticator")
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.SetBasicAuth("api", cnf.ApiKey)
|
|
|
|
|
2018-07-31 05:51:46 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
// Security XXX
|
|
|
|
// we trust mailgun implicitly and this is just a demo
|
|
|
|
// hence no DoS check on body size for now
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 || "{" != string(body[0]) {
|
|
|
|
fmt.Fprintf(os.Stdout, "[Mailgun] Uh-oh...\n[Maigun] Baby Brent says: %s\n", body)
|
|
|
|
}
|
|
|
|
|
|
|
|
return code, nil
|
2018-07-30 05:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var config Conf
|
2018-07-31 05:51:46 +00:00
|
|
|
|
2018-07-22 01:52:49 +00:00
|
|
|
func main() {
|
2018-07-31 05:51:46 +00:00
|
|
|
flag.Usage = usage
|
2018-08-02 08:13:56 +00:00
|
|
|
port := flag.Uint("port", 0, "tcp telnet chat port")
|
2018-07-31 05:51:46 +00:00
|
|
|
confname := flag.String("conf", "./config.yml", "yaml config file")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
confstr, err := ioutil.ReadFile(*confname)
|
|
|
|
fmt.Fprintf(os.Stdout, "-conf=%s\n", *confname)
|
|
|
|
if nil != err {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\nUsing defaults instead\n", err)
|
|
|
|
confstr = []byte("{\"port\":" + strconv.Itoa(int(*port)) + "}")
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(confstr, &config)
|
|
|
|
if nil != err {
|
|
|
|
config = Conf{}
|
|
|
|
}
|
2018-07-31 07:44:45 +00:00
|
|
|
if "" == config.RootPath {
|
|
|
|
// TODO Embed the public dir at the default
|
|
|
|
config.RootPath = "./public"
|
|
|
|
}
|
2018-07-31 05:51:46 +00:00
|
|
|
|
2018-08-02 01:13:10 +00:00
|
|
|
// The magical sorting hat
|
2018-08-02 07:09:34 +00:00
|
|
|
virginConns = make(chan net.Conn, 128)
|
2018-08-02 01:13:10 +00:00
|
|
|
|
|
|
|
// TCP & Authentication
|
2018-08-02 08:13:56 +00:00
|
|
|
telnetConns := make(map[bufferedConn]tcpUser)
|
2018-08-02 06:23:55 +00:00
|
|
|
wantsServerHello = make(chan bufferedConn, 128)
|
2018-08-02 07:34:00 +00:00
|
|
|
authTelnet = make(chan tcpUser, 128)
|
2018-08-02 01:13:10 +00:00
|
|
|
|
|
|
|
// HTTP & Authentication
|
|
|
|
myAuthReqs := make(map[string]authReq)
|
2018-08-01 02:35:40 +00:00
|
|
|
newAuthReqs = make(chan authReq, 128)
|
|
|
|
valAuthReqs = make(chan authReq, 128)
|
|
|
|
delAuthReqs = make(chan authReq, 128)
|
2018-08-02 06:23:55 +00:00
|
|
|
gotClientHello = make(chan bufferedConn, 128)
|
|
|
|
demuxHttpClient = make(chan bufferedConn, 128)
|
2018-07-31 05:51:46 +00:00
|
|
|
|
2018-08-02 01:13:10 +00:00
|
|
|
// cruft to delete
|
|
|
|
//myRooms = make(map[string](chan myMsg))
|
|
|
|
|
2018-07-31 05:51:46 +00:00
|
|
|
//myRooms["general"] = make(chan myMsg, 128)
|
2018-08-02 01:13:10 +00:00
|
|
|
// Note: I had considered dynamically select on channels for rooms.
|
|
|
|
// https://stackoverflow.com/questions/19992334/how-to-listen-to-n-channels-dynamic-select-statement
|
|
|
|
// I don't think that's actually the best approach, but I just wanted to save the link
|
|
|
|
|
|
|
|
broadcastMsg = make(chan myMsg, 128)
|
|
|
|
// Poor-Man's container/ring (circular buffer)
|
2018-08-02 06:23:55 +00:00
|
|
|
myChatHist.msgs = make([]*myMsg, 128)
|
2018-07-31 05:51:46 +00:00
|
|
|
|
|
|
|
var addr string
|
|
|
|
if 0 != int(*port) {
|
2018-08-02 08:13:56 +00:00
|
|
|
addr = config.Addr + ":" + strconv.Itoa(int(*port))
|
2018-07-31 05:51:46 +00:00
|
|
|
} else {
|
2018-08-02 08:13:56 +00:00
|
|
|
addr = config.Addr + ":" + strconv.Itoa(int(config.Port))
|
2018-07-31 05:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://golang.org/pkg/net/#Conn
|
|
|
|
sock, err := net.Listen("tcp", addr)
|
|
|
|
if nil != err {
|
2018-07-22 01:52:49 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Couldn't bind to TCP socket %q: %s\n", addr, err)
|
|
|
|
os.Exit(2)
|
2018-07-31 05:51:46 +00:00
|
|
|
}
|
|
|
|
fmt.Println("Listening on", addr)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := sock.Accept()
|
|
|
|
if err != nil {
|
|
|
|
// Not sure what kind of error this could be or how it could happen.
|
|
|
|
// Could a connection abort or end before it's handled?
|
|
|
|
fmt.Fprintf(os.Stderr, "Error accepting connection:\n%s\n", err)
|
|
|
|
}
|
2018-08-02 07:09:34 +00:00
|
|
|
virginConns <- conn
|
2018-07-31 05:51:46 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-07-31 07:44:45 +00:00
|
|
|
|
|
|
|
// Learning by Example
|
|
|
|
// https://github.com/emicklei/go-restful/blob/master/examples/restful-multi-containers.go
|
|
|
|
// https://github.com/emicklei/go-restful/blob/master/examples/restful-basic-authentication.go
|
|
|
|
// https://github.com/emicklei/go-restful/blob/master/examples/restful-serve-static.go
|
|
|
|
// https://github.com/emicklei/go-restful/blob/master/examples/restful-pre-post-filters.go
|
|
|
|
container := restful.NewContainer()
|
|
|
|
|
|
|
|
wsStatic := new(restful.WebService)
|
|
|
|
wsStatic.Path("/")
|
|
|
|
wsStatic.Route(wsStatic.GET("/").To(serveStatic))
|
|
|
|
wsStatic.Route(wsStatic.GET("/{subpath:*}").To(serveStatic))
|
|
|
|
container.Add(wsStatic)
|
|
|
|
|
2018-08-01 02:35:40 +00:00
|
|
|
cors := restful.CrossOriginResourceSharing{ExposeHeaders: []string{"Authorization"}, CookiesAllowed: false, Container: container}
|
2018-07-31 07:44:45 +00:00
|
|
|
wsApi := new(restful.WebService)
|
2018-08-01 02:35:40 +00:00
|
|
|
wsApi.Path("/api").Consumes(restful.MIME_JSON).Produces(restful.MIME_JSON).Filter(cors.Filter)
|
|
|
|
wsApi.Route(wsApi.GET("/hello").To(serveHello))
|
|
|
|
wsApi.Route(wsApi.POST("/sessions").To(requestAuth))
|
|
|
|
wsApi.Route(wsApi.POST("/sessions/{cid}").To(issueToken))
|
2018-08-02 08:13:56 +00:00
|
|
|
wsApi.Route(wsApi.GET("/rooms/{room}").Filter(requireToken).To(listMsgs))
|
|
|
|
wsApi.Route(wsApi.POST("/rooms/{room}").Filter(requireToken).To(postMsg))
|
2018-07-31 07:44:45 +00:00
|
|
|
container.Add(wsApi)
|
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Handler: container,
|
|
|
|
}
|
2018-08-02 06:23:55 +00:00
|
|
|
myHttpServer := newHttpServer(sock)
|
2018-07-31 07:44:45 +00:00
|
|
|
go func() {
|
|
|
|
server.Serve(myHttpServer)
|
|
|
|
}()
|
2018-07-31 05:51:46 +00:00
|
|
|
|
|
|
|
// Main event loop handling most access to shared data
|
|
|
|
for {
|
|
|
|
select {
|
2018-08-02 07:09:34 +00:00
|
|
|
case conn := <-virginConns:
|
2018-07-31 05:51:46 +00:00
|
|
|
// This is short lived
|
|
|
|
go handleConnection(conn)
|
2018-08-02 07:34:00 +00:00
|
|
|
case u := <-authTelnet:
|
2018-07-31 05:51:46 +00:00
|
|
|
// allow to receive messages
|
|
|
|
// (and be counted among the users)
|
2018-08-02 08:13:56 +00:00
|
|
|
telnetConns[u.bufConn] = u
|
2018-07-31 05:51:46 +00:00
|
|
|
// is chan chan the right way to handle this?
|
2018-08-02 08:13:56 +00:00
|
|
|
u.userCount <- len(telnetConns)
|
2018-08-02 01:13:10 +00:00
|
|
|
broadcastMsg <- myMsg{
|
2018-07-31 05:51:46 +00:00
|
|
|
sender: nil,
|
|
|
|
// TODO fmt.Fprintf()? template?
|
2018-08-02 01:13:10 +00:00
|
|
|
Message: "<" + u.email + "> joined #general\n",
|
|
|
|
ReceivedAt: time.Now(),
|
|
|
|
Channel: "general",
|
|
|
|
User: "system",
|
2018-07-31 05:51:46 +00:00
|
|
|
}
|
2018-08-01 02:35:40 +00:00
|
|
|
case ar := <-newAuthReqs:
|
|
|
|
myAuthReqs[ar.Cid] = ar
|
2018-08-02 01:13:10 +00:00
|
|
|
case ar := <-valAuthReqs:
|
2018-08-01 02:35:40 +00:00
|
|
|
// TODO In this case it's probably more conventional (and efficient) to
|
|
|
|
// use struct with a mutex and the authReqs map than a chan chan
|
2018-08-02 01:13:10 +00:00
|
|
|
av, ok := myAuthReqs[ar.Cid]
|
|
|
|
//ar.Chan <- nil // TODO
|
|
|
|
if ok {
|
|
|
|
ar.Chan <- av
|
|
|
|
} else {
|
2018-08-02 07:13:26 +00:00
|
|
|
// sending empty object so that I can still send a copy
|
|
|
|
// rather than a pointer above. Maybe not the right way
|
|
|
|
// to do this, but it works for now.
|
2018-08-02 01:13:10 +00:00
|
|
|
ar.Chan <- authReq{}
|
|
|
|
}
|
2018-08-01 02:35:40 +00:00
|
|
|
case ar := <-delAuthReqs:
|
|
|
|
delete(myAuthReqs, ar.Cid)
|
2018-08-02 06:23:55 +00:00
|
|
|
case bufConn := <-wantsServerHello:
|
|
|
|
go handleTelnetConn(bufConn)
|
2018-08-02 07:34:00 +00:00
|
|
|
case u := <-cleanTelnet:
|
2018-08-02 06:23:55 +00:00
|
|
|
// we can safely ignore this error, if any
|
2018-08-02 08:13:56 +00:00
|
|
|
if "" != u.email {
|
|
|
|
broadcastMsg <- myMsg{
|
|
|
|
sender: nil,
|
|
|
|
// TODO fmt.Fprintf()? template?
|
|
|
|
Message: "<" + u.email + "> left #general\n",
|
|
|
|
ReceivedAt: time.Now(),
|
|
|
|
Channel: "general",
|
|
|
|
User: "system",
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 07:09:34 +00:00
|
|
|
close(u.newMsg)
|
2018-08-02 06:23:55 +00:00
|
|
|
u.bufConn.Close()
|
2018-08-02 08:13:56 +00:00
|
|
|
delete(telnetConns, u.bufConn)
|
2018-08-02 06:23:55 +00:00
|
|
|
case bufConn := <-gotClientHello:
|
|
|
|
go muxTcp(bufConn)
|
|
|
|
case bufConn := <-demuxHttpClient:
|
|
|
|
// this will be Accept()ed immediately by the go-restful container
|
2018-08-02 01:13:10 +00:00
|
|
|
// NOTE: we don't store these HTTP connections for broadcast
|
2018-08-02 06:23:55 +00:00
|
|
|
// since we manage the session by HTTP Auth Bearer rather than TCP
|
2018-07-31 07:44:45 +00:00
|
|
|
myHttpServer.chans <- bufConn
|
2018-08-02 01:13:10 +00:00
|
|
|
case msg := <-broadcastMsg:
|
2018-08-02 06:23:55 +00:00
|
|
|
// copy comes in, pointer gets saved (and not GC'd, I hope)
|
2018-08-02 06:33:11 +00:00
|
|
|
myChatHist.msgs[myChatHist.i] = &msg
|
2018-08-02 06:23:55 +00:00
|
|
|
myChatHist.i += 1
|
|
|
|
if myChatHist.c < cap(myChatHist.msgs) {
|
|
|
|
myChatHist.c += 1
|
|
|
|
}
|
|
|
|
myChatHist.i %= len(myChatHist.msgs)
|
2018-08-02 01:13:10 +00:00
|
|
|
|
2018-08-02 06:23:55 +00:00
|
|
|
// print the system message (the "log")
|
2018-08-02 01:13:10 +00:00
|
|
|
t := msg.ReceivedAt
|
2018-07-31 05:51:46 +00:00
|
|
|
tf := "%d-%02d-%02d %02d:%02d:%02d (%s)"
|
|
|
|
var sender string
|
|
|
|
if nil != msg.sender {
|
|
|
|
sender = msg.sender.RemoteAddr().String()
|
|
|
|
} else {
|
|
|
|
sender = "system"
|
|
|
|
}
|
2018-08-02 01:13:10 +00:00
|
|
|
// Tangential thought:
|
2018-07-31 05:51:46 +00:00
|
|
|
// I wonder if we could use IP detection to get the client's tz
|
|
|
|
// ... could probably make time for this in the authentication loop
|
2018-08-02 01:13:10 +00:00
|
|
|
zone, _ := msg.ReceivedAt.Zone()
|
2018-08-02 07:34:00 +00:00
|
|
|
fmt.Fprintf(os.Stdout, tf+" [%s] (%s): %s\r\n",
|
2018-07-31 05:51:46 +00:00
|
|
|
t.Year(), t.Month(), t.Day(),
|
|
|
|
t.Hour(), t.Minute(), t.Second(), zone,
|
|
|
|
sender,
|
2018-08-02 01:13:10 +00:00
|
|
|
msg.User, msg.Message)
|
2018-07-31 05:51:46 +00:00
|
|
|
|
2018-08-02 08:13:56 +00:00
|
|
|
for _, u := range telnetConns {
|
2018-07-31 05:51:46 +00:00
|
|
|
// Don't echo back to the original client
|
2018-08-02 06:23:55 +00:00
|
|
|
if msg.sender == u.bufConn {
|
2018-07-31 05:51:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-02 06:23:55 +00:00
|
|
|
msg := fmt.Sprintf(tf+" [%s]: %s", t.Year(), t.Month(), t.Day(), t.Hour(),
|
|
|
|
t.Minute(), t.Second(), zone, msg.User, msg.Message)
|
|
|
|
select {
|
|
|
|
case u.newMsg <- msg:
|
|
|
|
// all is well, client was ready to receive
|
|
|
|
default:
|
|
|
|
// Rate Limit: Reasonable poor man's DoS prevention (Part 2)
|
|
|
|
// This client's send channel buffer is full.
|
|
|
|
// It is consuming data too slowly. It may be malicious.
|
|
|
|
// In the case that it's experiencing network issues,
|
|
|
|
// well, these things happen when you're having network issues.
|
|
|
|
// It can reconnect.
|
2018-08-02 07:34:00 +00:00
|
|
|
cleanTelnet <- u
|
2018-08-02 06:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// To ask: Why do I have to pass in conn to prevent a data race? Is it garbage collection?
|
|
|
|
// Don't block the rest of the loop
|
|
|
|
// TODONE maybe use a chan to send to the socket's event loop
|
|
|
|
// (left this in to remind myself to ask questions)
|
|
|
|
go func(conn bufferedConn) {
|
|
|
|
// Protect against malicious clients to prevent DoS
|
|
|
|
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
|
|
|
|
timeoutDuration := 2 * time.Second
|
|
|
|
conn.SetWriteDeadline(time.Now().Add(timeoutDuration))
|
|
|
|
_, err := fmt.Fprintf(conn, msg)
|
|
|
|
if nil != err {
|
2018-08-02 07:34:00 +00:00
|
|
|
cleanTelnet <- u
|
2018-08-02 06:23:55 +00:00
|
|
|
}
|
|
|
|
}(conn)
|
|
|
|
*/
|
2018-07-31 05:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 01:52:49 +00:00
|
|
|
}
|