reorganize code

This commit is contained in:
Zack Scholl 2019-07-10 16:27:40 -07:00
parent 8a1b684a03
commit e77c845b47
3 changed files with 137 additions and 136 deletions

View file

@ -1,43 +0,0 @@
package main
import (
"github.com/gorilla/websocket"
log "github.com/schollz/logger"
)
type Client struct {
WebsocketURL string
}
func (c *Client) Run() (err error) {
log.Debugf("dialing %s", c.WebsocketURL)
wsDial, _, err := websocket.DefaultDialer.Dial(c.WebsocketURL, nil)
if err != nil {
log.Error(err)
return
}
defer wsDial.Close()
ws := NewWebsocket(wsDial)
err = ws.Send(Payload{
Type:"domain",
Message:"zack",
})
if err != nil {
log.Error(err)
return
}
for {
var p Payload
p, err = ws.Receive()
if err != nil {
log.Debug(err)
return
}
log.Debugf("recv: %+v", p)
}
return
}

140
main.go
View file

@ -2,10 +2,13 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
@ -131,6 +134,7 @@ type Connection struct {
ID int
Joined time.Time
Domain string
Key string
LastGet string
ws *WebsocketConn
}
@ -272,7 +276,7 @@ Disallow: /`))
pathToFile += "/"
}
pathToFile += "index.html"
data, err = s.get(pathToFile, ipAddress)
data, err = s.get(domain, pathToFile, ipAddress)
if err != nil {
return
}
@ -389,7 +393,7 @@ func (s *server) get(domain, filePath, ipAddress string) (payload string, err er
for _, i := range rand.Perm(len(connections)) {
var p Payload
p, err = func() (p Payload, err error) {
err = conn.ws.Send(Payload{
err = connections[i].ws.Send(Payload{
Type: "get",
Message: filePath,
IPAddress: ipAddress,
@ -402,7 +406,7 @@ func (s *server) get(domain, filePath, ipAddress string) (payload string, err er
}()
if err != nil {
log.Debug(err)
s.DumpConnection(domain, conn.ID)
s.DumpConnection(domain, connections[i].ID)
continue
}
if len(p.Message) > 10 {
@ -444,3 +448,133 @@ func (s *server) DumpConnection(domain string, id int) (err error) {
func remove(slice []*Connection, s int) []*Connection {
return append(slice[:s], slice[s+1:]...)
}
//
// client code
//
type Client struct {
WebsocketURL string
Domain string
Key string
}
func (c *Client) Run() (err error) {
log.Debugf("dialing %s", c.WebsocketURL)
wsDial, _, err := websocket.DefaultDialer.Dial(c.WebsocketURL, nil)
if err != nil {
log.Error(err)
return
}
defer wsDial.Close()
ws := NewWebsocket(wsDial)
err = ws.Send(Payload{
Type: "domain",
Message: c.Domain,
Key: c.Key,
})
if err != nil {
log.Error(err)
return
}
for {
var p Payload
p, err = ws.Receive()
if err != nil {
log.Debug(err)
return
}
log.Debugf("recv: %+v", p)
}
return
}
//
// utility functions
//
// GetClientIPHelper gets the client IP using a mixture of techniques.
// This is how it is with golang at the moment.
func GetClientIPHelper(req *http.Request) (ipResult string, errResult error) {
// Try lots of ways :) Order is important.
// Try Request Headers (X-Forwarder). Client could be behind a Proxy
ip, err := getClientIPByHeaders(req)
if err == nil {
// log.Printf("debug: Found IP using Request Headers sniffing. ip: %v", ip)
return ip, nil
}
// Try by Request
ip, err = getClientIPByRequestRemoteAddr(req)
if err == nil {
// log.Printf("debug: Found IP using Request sniffing. ip: %v", ip)
return ip, nil
}
// Try Request Header ("Origin")
url, err := url.Parse(req.Header.Get("Origin"))
if err == nil {
host := url.Host
ip, _, err := net.SplitHostPort(host)
if err == nil {
// log.Printf("debug: Found IP using Header (Origin) sniffing. ip: %v", ip)
return ip, nil
}
}
err = errors.New("error: Could not find clients IP address")
return "", err
}
// getClientIPByRequest tries to get directly from the Request.
// https://blog.golang.org/context/userip/userip.go
func getClientIPByRequestRemoteAddr(req *http.Request) (ip string, err error) {
// Try via request
ip, _, err = net.SplitHostPort(req.RemoteAddr)
if err != nil {
// log.Printf("debug: Getting req.RemoteAddr %v", err)
return "", err
} else {
// log.Printf("debug: With req.RemoteAddr found IP:%v; Port: %v", ip, port)
}
userIP := net.ParseIP(ip)
if userIP == nil {
message := fmt.Sprintf("debug: Parsing IP from Request.RemoteAddr got nothing.")
// log.Printf(message)
return "", fmt.Errorf(message)
}
// log.Printf("debug: Found IP: %v", userIP)
return userIP.String(), nil
}
// getClientIPByHeaders tries to get directly from the Request Headers.
// This is only way when the client is behind a Proxy.
func getClientIPByHeaders(req *http.Request) (ip string, err error) {
// Client could be behid a Proxy, so Try Request Headers (X-Forwarder)
ipSlice := []string{}
ipSlice = append(ipSlice, req.Header.Get("X-Forwarded-For"))
ipSlice = append(ipSlice, req.Header.Get("x-forwarded-for"))
ipSlice = append(ipSlice, req.Header.Get("X-FORWARDED-FOR"))
for _, v := range ipSlice {
// log.Printf("debug: client request header check gives ip: %v", v)
if v != "" {
return v, nil
}
}
err = errors.New("error: Could not find clients IP address from the Request Headers")
return "", err
}

View file

@ -1,90 +0,0 @@
package main
import (
"errors"
"fmt"
"net"
"net/http"
"net/url"
)
// GetClientIPHelper gets the client IP using a mixture of techniques.
// This is how it is with golang at the moment.
func GetClientIPHelper(req *http.Request) (ipResult string, errResult error) {
// Try lots of ways :) Order is important.
// Try Request Headers (X-Forwarder). Client could be behind a Proxy
ip, err := getClientIPByHeaders(req)
if err == nil {
// log.Printf("debug: Found IP using Request Headers sniffing. ip: %v", ip)
return ip, nil
}
// Try by Request
ip, err = getClientIPByRequestRemoteAddr(req)
if err == nil {
// log.Printf("debug: Found IP using Request sniffing. ip: %v", ip)
return ip, nil
}
// Try Request Header ("Origin")
url, err := url.Parse(req.Header.Get("Origin"))
if err == nil {
host := url.Host
ip, _, err := net.SplitHostPort(host)
if err == nil {
// log.Printf("debug: Found IP using Header (Origin) sniffing. ip: %v", ip)
return ip, nil
}
}
err = errors.New("error: Could not find clients IP address")
return "", err
}
// getClientIPByRequest tries to get directly from the Request.
// https://blog.golang.org/context/userip/userip.go
func getClientIPByRequestRemoteAddr(req *http.Request) (ip string, err error) {
// Try via request
ip, _, err = net.SplitHostPort(req.RemoteAddr)
if err != nil {
// log.Printf("debug: Getting req.RemoteAddr %v", err)
return "", err
} else {
// log.Printf("debug: With req.RemoteAddr found IP:%v; Port: %v", ip, port)
}
userIP := net.ParseIP(ip)
if userIP == nil {
message := fmt.Sprintf("debug: Parsing IP from Request.RemoteAddr got nothing.")
// log.Printf(message)
return "", fmt.Errorf(message)
}
// log.Printf("debug: Found IP: %v", userIP)
return userIP.String(), nil
}
// getClientIPByHeaders tries to get directly from the Request Headers.
// This is only way when the client is behind a Proxy.
func getClientIPByHeaders(req *http.Request) (ip string, err error) {
// Client could be behid a Proxy, so Try Request Headers (X-Forwarder)
ipSlice := []string{}
ipSlice = append(ipSlice, req.Header.Get("X-Forwarded-For"))
ipSlice = append(ipSlice, req.Header.Get("x-forwarded-for"))
ipSlice = append(ipSlice, req.Header.Get("X-FORWARDED-FOR"))
for _, v := range ipSlice {
// log.Printf("debug: client request header check gives ip: %v", v)
if v != "" {
return v, nil
}
}
err = errors.New("error: Could not find clients IP address from the Request Headers")
return "", err
}