Compare commits

..

No commits in common. "master" and "f437bfec59f35a03f0a19a3ef591047c84c1ca5d" have entirely different histories.

26 changed files with 54 additions and 2035 deletions

5
.gitignore vendored
View File

@ -1,7 +1,2 @@
*.swp
server/server
client/client
example-protobuf/example-protobuf
gorilla-server/gorilla-server
emaild/emaild
goclient/goclient

View File

@ -1,13 +1,3 @@
# Intro
Attempt at some code to validate via DNS / DNSSEC
the hostname is valid on a simple socket listen
server
This recursively runs from the root TLD's and purposesly bypasses
any DNS cache so that the DNS <-> IP address mappings can be verified
## References
* https://github.com/mactsouk/opensource.com.git
* https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go

44
args.go
View File

@ -1,44 +0,0 @@
package dnssecsocket
//
// By using the package "github.com/alexflint/go-arg",
// these can be configured from the command line
//
import (
// arg "github.com/alexflint/go-arg"
// "log"
// "os"
)
type Args struct {
VerboseDnssec bool `arg:"--verbose-dnssec" help:"debug dnssec lookups"`
Foo string `arg:"env:USER"`
}
var args struct {
Args
Verbose bool
}
func Parse (b bool) {
args.Verbose = b
args.VerboseDnssec = b
}
// I attempted to pass the *arg.Parser down
// to see if I could find the value somewhere but I couldn't find it
/*
var conf arg.Config
func Parse (p *arg.Parser) {
// conf.Program = "control-panel-dns"
// conf.IgnoreEnv = false
// arg.NewParser(conf, &args)
log.Println("fuckit", p, args.VerboseDnssec)
for i, v := range p.SubcommandNames() {
log.Println("dnssec.Parse", i, v)
}
p.Jcarr()
}
*/

View File

@ -5,16 +5,16 @@
package dnssecsocket
import "os"
// import "fmt"
// import "os"
import "bufio"
import "math/rand"
import "net"
import "strconv"
import "strings"
// import log "github.com/sirupsen/logrus"
// import "github.com/wercker/journalhook"
import "git.wit.org/wit/shell"
// import "time"
import "log"
// will try to get this hosts FQDN
// import "github.com/Showmax/go-fqdn"
@ -30,8 +30,8 @@ func random() int {
return rand.Intn(MAX-MIN) + MIN
}
func GetRemoteAddr(conn net.TCPConn) string {
clientAddr := conn.RemoteAddr().String()
func GetRemoteAddr(c net.Conn) string {
clientAddr := c.RemoteAddr().String()
parts := strings.Split(clientAddr, "]")
ipv6 := parts[0]
return ipv6[1:]
@ -42,66 +42,37 @@ func GetRemoteAddr(conn net.TCPConn) string {
// Each client must send it's hostname as the first line
// Then each hostname is verified with DNSSEC
//
func HandleConnection(conn *net.TCPConn) {
// Disable journalhook until it builds on Windows
// journalhook.Enable()
// spew.Dump(conn)
// ipv6client := GetRemoteAddr(c)
ipv6client := conn.RemoteAddr()
log(args.VerboseDnssec, "Serving to %s as the IPv6 client", ipv6client)
// setup this TCP socket as the "standard input"
// newStdin, _ := bufio.NewReader(conn.File())
newStdin, _ := conn.File()
newreader := bufio.NewReader(newStdin)
log(args.VerboseDnssec, "Waiting for the client to tell me its name")
netData, err := newreader.ReadString('\n')
func HandleConnection(c net.Conn) {
ipv6client := GetRemoteAddr(c)
log.Println("Serving to %s as the IPv6 client", ipv6client)
log.Println("Waiting for the client to tell me its name")
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
log(args.VerboseDnssec, err)
log.Println(err)
return
}
clientHostname := strings.TrimSpace(netData)
log(args.VerboseDnssec, "Recieved client hostname as:", clientHostname)
log.Println("Recieved client hostname as:", clientHostname)
dnsRR := Dnstrace(clientHostname, "AAAA")
if (dnsRR == nil) {
log(args.VerboseDnssec, "dnsRR IS NIL")
log(args.VerboseDnssec, "dnsRR IS NIL")
log(args.VerboseDnssec, "dnsRR IS NIL")
conn.Close()
return
}
ipaddr := dns.Field(dnsRR[1], 1)
log(args.VerboseDnssec, "Client claims to be: ", ipaddr)
log(args.VerboseDnssec, "Serving to IPv6 client:", ipv6client)
/* TODO: figure out how to fix this check
ipaddr := dns.Field(dnsRR, 1)
log.Println("Client claims to be: ", ipaddr)
log.Println("Serving to IPv6 client:", ipv6client)
if (ipaddr != ipv6client) {
log(args.VerboseDnssec)
log(args.VerboseDnssec, "DNSSEC ERROR: client IPv6 does not work")
log(args.VerboseDnssec, "DNSSEC ERROR: client IPv6 does not work")
log(args.VerboseDnssec, "DNSSEC ERROR: client IPv6 does not work")
log(args.VerboseDnssec)
conn.Close()
log.Println()
log.Println("DNSSEC ERROR: client IPv6 does not work")
log.Println("DNSSEC ERROR: client IPv6 does not work")
log.Println("DNSSEC ERROR: client IPv6 does not work")
log.Println()
c.Close()
return
}
*/
f, _ := conn.File()
// shell.SetStdout(f)
// shell.SpewOn() // turn this on if you want to look at the process exit states
// send all log() output to systemd journalctl
// shell.UseJournalctl()
for {
defer shell.SetStdout(os.Stdout)
defer conn.Close()
netData, err := newreader.ReadString('\n')
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
log(args.VerboseDnssec, err)
log.Println(err)
return
}
@ -109,23 +80,10 @@ func HandleConnection(conn *net.TCPConn) {
if temp == "STOP" {
break
}
log(args.VerboseDnssec, "Recieved: ", temp)
if (temp == "list") {
log(args.VerboseDnssec, "Should run list here")
shell.SetStdout(f)
shell.Run("/root/bin/list.testing.com")
shell.SetStdout(os.Stdout)
}
if (temp == "cpuinfo") {
log(args.VerboseDnssec, "Should cat /proc/cpuinfo")
shell.SetStdout(f)
shell.Run("cat /proc/cpuinfo")
shell.SetStdout(os.Stdout)
}
log.Println("Recieved: ", temp)
result := strconv.Itoa(random()) + "\n"
conn.Write([]byte(string(result)))
c.Write([]byte(string(result)))
}
c.Close()
}

View File

@ -3,19 +3,15 @@ package dnssecsocket
// inspired from github.com/rs/dnstrace/main.go
import "fmt"
import "log"
import "net"
// import "os"
import "os"
import "strings"
import "time"
import "github.com/miekg/dns"
import "github.com/rs/dnstrace/client"
// import log "github.com/sirupsen/logrus"
// this is cool, but breaks the Windows build
// import "github.com/wercker/journalhook"
// import "github.com/davecgh/go-spew/spew"
const (
@ -38,7 +34,7 @@ func colorize(s interface{}, color int, enabled bool) string {
return fmt.Sprintf("\x1b[%dm%v\x1b[0m", color, s)
}
func Dnstrace(hostname string, qtypestr string) []dns.RR {
func Dnstrace(hostname string, qtypestr string) dns.RR {
// color := flag.Bool("color", true, "Enable/disable colors")
color := true
@ -63,7 +59,7 @@ func Dnstrace(hostname string, qtypestr string) []dns.RR {
o.SetUDPSize(dns.DefaultMsgSize)
m.Extra = append(m.Extra, o)
c := client.New(1)
c := client.New()
c.Client.Timeout = 500 * time.Millisecond
t := client.Tracer{
GotIntermediaryResponse: func(i int, m *dns.Msg, rs client.Responses, rtype client.ResponseType) {
@ -75,13 +71,13 @@ func Dnstrace(hostname string, qtypestr string) []dns.RR {
qname := m.Question[0].Name
qtype := dns.TypeToString[m.Question[0].Qtype]
if i > 1 {
log(args.VerboseDnssec)
fmt.Println()
}
log(args.VerboseDnssec, "%d - query %s %s", i, qtype, qname)
fmt.Printf("%d - query %s %s", i, qtype, qname)
if r != nil {
log(args.VerboseDnssec, ": %s", strings.Replace(strings.Replace(r.MsgHdr.String(), ";; ", "", -1), "\n", ", ", -1))
fmt.Printf(": %s", strings.Replace(strings.Replace(r.MsgHdr.String(), ";; ", "", -1), "\n", ", ", -1))
}
log(args.VerboseDnssec)
fmt.Println()
for _, pr := range rs {
ln := 0
if pr.Msg != nil {
@ -94,15 +90,15 @@ func Dnstrace(hostname string, qtypestr string) []dns.RR {
} else if pr.Server.LookupRTT > 0 {
lrtt = fmt.Sprintf("%.2fms", float64(pr.Server.LookupRTT)/float64(time.Millisecond))
}
log(args.VerboseDnssec, col(" - %d bytes in %.2fms + %s lookup on %s(%s)", cDarkGray), ln, rtt, lrtt, pr.Server.Name, pr.Addr)
fmt.Printf(col(" - %d bytes in %.2fms + %s lookup on %s(%s)", cDarkGray), ln, rtt, lrtt, pr.Server.Name, pr.Addr)
if pr.Err != nil {
err := pr.Err
if oerr, ok := err.(*net.OpError); ok {
err = oerr.Err
}
log(args.VerboseDnssec, ": %v", col(err, cRed))
fmt.Printf(": %v", col(err, cRed))
}
log(args.VerboseDnssec, "\n")
fmt.Print("\n")
}
switch rtype {
@ -122,47 +118,31 @@ func Dnstrace(hostname string, qtypestr string) []dns.RR {
} else {
glue = col("no glue", cYellow)
}
log(args.VerboseDnssec, "%s %d NS %s (%s)\n", label, s.TTL, s.Name, glue)
fmt.Printf("%s %d NS %s (%s)\n", label, s.TTL, s.Name, glue)
}
case client.ResponseTypeCNAME:
for _, rr := range r.Answer {
log(args.VerboseDnssec, rr)
fmt.Println(rr)
}
}
},
FollowingCNAME: func(domain, target string) {
log(args.VerboseDnssec, col("\n~ following CNAME %s -> %s\n", cBlue), domain, target)
fmt.Printf(col("\n~ following CNAME %s -> %s\n", cBlue), domain, target)
},
}
r, rtt, err := c.RecursiveQuery(m, t)
if err != nil {
log(args.VerboseDnssec, col("*** error: %v\n", cRed), err)
return nil
fmt.Printf(col("*** error: %v\n", cRed), err)
os.Exit(1)
}
log(args.VerboseDnssec)
log(args.VerboseDnssec, col(";; Cold best path time: %s\n\n", cGray), rtt)
for i, rr := range r.Answer {
log(args.VerboseDnssec, "r.Answer =", i, rr, args.VerboseDnssec)
fmt.Println()
fmt.Printf(col(";; Cold best path time: %s\n\n", cGray), rtt)
for _, rr := range r.Answer {
log.Println(rr)
}
return r.Answer
// for _, rr := range r.Answer {
// return rr
// }
// return nil
}
func ResolveIPv6hostname(hostname string) *net.TCPAddr {
dnsRR := Dnstrace(hostname, "AAAA")
if (dnsRR == nil) {
return nil
for _, rr := range r.Answer {
return rr
}
aaaa := dns.Field(dnsRR[1], 1)
localTCPAddr, _ := net.ResolveTCPAddr("tcp", aaaa)
return localTCPAddr
}
func UseJournalctl() {
log(args.VerboseDnssec, "journalhook is disabled because it breaks the Windows build right now")
// journalhook.Enable()
return nil
}

View File

@ -1,13 +0,0 @@
# do this
all:
go run alfonso.go
# this is the raw socket server. it will
# show all socket connects recieved
raw-socket:
go run server.go
build:
go build
./emaild

View File

@ -1,76 +0,0 @@
package main
import "fmt"
import "log"
import "net/http"
import "io/ioutil"
import "strings"
import "os"
import "time"
import "github.com/davecgh/go-spew/spew"
import "github.com/golang/protobuf/jsonpb"
import pb "git.wit.com/wit/witProtobuf"
func handler(w http.ResponseWriter, r *http.Request) {
// spew.Dump(r)
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
log.Printf("handler: fucking unknown path")
}
func JSONtoPB(a string) pb.Account {
sets := pb.Account{}
err := jsonpb.Unmarshal(strings.NewReader(string(a)), &sets)
if err != nil {
log.Println("jsonpb.Unmarshal() ERROR =", err)
}
spew.Dump(sets)
return sets
}
func main() {
log.Println("listen on :9000")
http.HandleFunc("/", handler)
http.HandleFunc("/email", handler2)
http.ListenAndServe(":9000", nil)
}
func writeToFile(filename string, a string) {
f, _ := os.Create(filename)
f.WriteString(a)
f.Close()
}
func handler2(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
// spew.Dump(r)
fmt.Fprintf(w, "Hi there, handler2 %s!", r.URL.Path[1:])
body, err := ioutil.ReadAll(r.Body)
j := string(body)
log.Println("body =", j)
log.Println("err =", err)
// change the JSON int a protobuf
account := JSONtoPB(string(body))
account.Token = "testing a token"
log.Println("account =", account)
// change it back to JSON
marshaler := &jsonpb.Marshaler{}
stuff, _ := marshaler.MarshalToString(&account)
log.Println(stuff)
current := time.Now()
filename := "/tmp/" + current.Format("2006-01-02-15-04-05")
log.Println("filename =", filename)
// write to file
writeToFile(filename, stuff + "\n")
}

View File

@ -1,268 +0,0 @@
package main
import "flag"
import "io"
import "log"
import "net/http"
import "encoding/json"
import "io/ioutil"
import "github.com/golang/protobuf/proto"
import pb "git.wit.com/wit/witProtobuf"
import "github.com/gorilla/websocket"
import "github.com/davecgh/go-spew/spew"
import "github.com/tidwall/gjson"
const (
API = "http://stackapi:4000/"
MAX_NETS = 10
MAX_VMS = 10
)
type network struct {
Subdomain string `json:"subdomain"`
}
func checkLogin(ge *pb.Event) bool {
req, err := http.NewRequest("GET", API + "me", nil)
if err != nil {
log.Println("can't make new req")
return false
}
req.Header.Set("X-Wit-Auth", ge.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Can't exec the req to list networks: %s", err)
return false
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
return false
}
log.Printf("RESPONSE =", body)
if body == nil {
log.Println("RESPONSE is nil")
return false
} else {
len := len(body)
log.Println("body = ", string(body))
log.Println("body len = ", len)
email := gjson.Get(string(body), "email")
log.Println("email = ", email.String())
log.Println("ge.Username = ", ge.Username)
if (email.String() == ge.Username) {
log.Println("\tTHIS TOTALLY WORKED MAN")
return true
}
}
return false
}
func jsonAddEvent(ge *pb.Event) bool {
var url string
var httpType string
httpType = "GET"
if ge.Type == pb.Event_LOGIN {
url = API + "me"
} else if ge.Type == pb.Event_ADD {
// stackapi:4000/vms/jcarr.com?count=1&cpu=2&ram=512&disk=25
url = API + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
httpType = "POST"
} else if ge.Type == pb.Event_MIGRATE {
url = API + "clusters"
} else {
// if unknown, just check login
url = API + "me"
}
log.Println("jsonAddEvent() ", httpType, url)
req, err := http.NewRequest(httpType, url, nil)
if err != nil {
log.Println("can't make new req")
return false
}
req.Header.Set("X-Wit-Auth", ge.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Can't exec the req to list networks: %s", err)
return false
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
return false
}
log.Printf("RESPONSE =", body)
if body == nil {
log.Println("RESPONSE is nil")
return false
} else {
len := len(body)
log.Println("body = ", string(body))
log.Println("body len = ", len)
email := gjson.Get(string(body), "email")
log.Println("email = ", email.String())
log.Println("ge.Username = ", ge.Username)
if (email.String() == ge.Username) {
log.Println("\tTHIS TOTALLY WORKED MAN")
return true
}
}
return false
}
func fetchNetworks(token string) []network {
var networks []network
req, err := http.NewRequest("GET", API + "clusters", nil)
if err != nil { log.Fatal("can't make new req") }
req.Header.Set("X-Wit-Auth", token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { log.Printf("Can't exec the req to list networks: %s", err) }
body, err := ioutil.ReadAll(resp.Body)
if err != nil { log.Fatal("can't read resp") }
log.Printf("NETWORKS =", body)
if body == nil {
log.Println("networks is nil")
} else {
len := len(body)
err = json.Unmarshal(body, &networks)
if err != nil {
log.Println("body = ", body)
log.Println("body len = ", len)
log.Println("can't unmarshal resp for list networks: %s", err)
}
}
spew.Dump(networks)
return networks
}
/*
{
"name": "vm10",
"cpu": 4,
"ram": 2048,
"created_on": "2019-03-18T19:22:10.755428Z",
"scope": "private",
"role": "k8s-node",
"stage": "Creating...",
"seconds_left": 300,
"base_image": "debcore_guest_latest",
"address_list": [
"192.168.0.1/32"
]
},
*/
type address struct {
Address string
}
type vm struct {
Name string `json:"name"`
Role string `json:"role"`
BaseImage string `json:"base_image"`
}
func fetchVMs(token string, network string) []vm {
var vms []vm
req, err := http.NewRequest("GET", API + "vms/" + network, nil)
if err != nil { log.Fatal("can't make new req") }
req.Header.Set("X-Wit-Auth", token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { log.Printf("Can't exec the req to fetch vms for %s: %s", network, err) }
body, err := ioutil.ReadAll(resp.Body)
if err != nil { log.Fatal("can't read resp") }
err = json.Unmarshal(body, &vms)
if err != nil { log.Fatal("can't unmarshal resp for fetch vms: ", err, "\n\n", network, string(body)) }
spew.Dump(vms)
return vms
}
// Also potentially look at:
// https://www.reddit.com/r/golang/comments/boznnz/an_alternative_to_gorilla_sessions_jeff_manage/
var upgrader = websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
EnableCompression: true,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func eventHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Starting http.ListenAndServe() on port 9000")
spew.Dump(r)
log.Println("echoHandler() START")
log.Println("echoHandler() ATTEMPT r.Header.Get", r.Header.Get("User-Agent"))
log.Println("echoHandler() ATTEMPT r.Header", r.Header)
for key, value := range r.Header {
log.Println("echoHandler() HEADER key, value =", key, value)
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade:", err)
return
}
defer conn.Close()
for {
mytype, message, err := conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
if mytype == websocket.TextMessage {
log.Println("echoHandler() TextMessage mytype=", mytype)
}
if mytype == websocket.BinaryMessage {
log.Println("echoHandler() BinaryMessage mytype=", mytype)
pdata := new(pb.Event)
err = proto.Unmarshal(message, pdata)
if (err != nil) {
log.Printf("readConn() something fucked up happened in Unmarshal")
}
log.Printf("recv binary: %s", pdata)
}
}
}
func serveHome(w http.ResponseWriter, r *http.Request) {
log.Println("Starting http.ListenAndServe() on port 9000")
spew.Dump(r)
if r.URL.Path != "/" {
http.Error(w, "Not found.", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, "<html><body>Echo Server</body></html>")
}
var addr = flag.String("addr", ":9000", "http service address")
func main() {
flag.Parse()
http.HandleFunc("/", serveHome)
// http.HandleFunc("/event", eventHandler)
http.HandleFunc("/email", eventHandler)
log.Println("Starting http.ListenAndServe() on port 9000")
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

View File

@ -1,118 +0,0 @@
// inspired from:
// https://github.com/mactsouk/opensource.com.git
// and
// https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go
package main
// import "fmt"
// import "os"
import "math/rand"
import "net"
import "time"
import "log"
import "bufio"
import "strings"
import "strconv"
import "github.com/davecgh/go-spew/spew"
const (
CONN_HOST = "localhost"
CONN_PORT = "9000"
CONN_TYPE = "tcp"
)
/*
// the simplest handler of them all
// just write everything from the socket to stdout
go func(c net.Conn) {
defer c.Close()
io.Copy(os.Stdout, c)
}(conn)
*/
func random() int {
return rand.Intn(50) + 4
}
func main() {
listenstr := "[" + "localhost" + "]:" + CONN_PORT
log.Println("listenstr", listenstr)
myTCPAddr, err := net.ResolveTCPAddr("tcp", listenstr)
// // Listen for incoming connections on the IPv6 address only
l, err := net.ListenTCP("tcp", myTCPAddr)
if err != nil {
log.Println("Error listening:", err.Error())
return
}
// Close the listener when the application closes.
defer l.Close()
log.Println("Listening on " + listenstr)
rand.Seed(time.Now().Unix())
for {
// Listen for an incoming connection.
conn, err := l.AcceptTCP()
if err != nil {
log.Println("Error accepting: ", err.Error())
return
}
log.Println("Accepted new connection from: " + conn.RemoteAddr().String())
// Handle connections in a new goroutine.
go HandleConnection(conn)
}
}
//
// Handle each connection
// Each client must send it's hostname as the first line
// Then each hostname is verified with DNSSEC
//
func HandleConnection(conn *net.TCPConn) {
ipv6client := conn.RemoteAddr()
log.Println("Serving to %s as the IPv6 client", ipv6client)
// setup this TCP socket as the "standard input"
// newStdin, _ := bufio.NewReader(conn.File())
newStdin, _ := conn.File()
newreader := bufio.NewReader(newStdin)
log.Println("Waiting for the client to tell me its name")
netData, err := newreader.ReadString('\n')
spew.Dump(netData)
log.Println("netData =", string(netData))
if err != nil {
log.Println(err)
return
}
// f, _ := conn.File()
for {
defer conn.Close()
netData, err := newreader.ReadString('\n')
if err != nil {
log.Println(err)
return
}
temp := strings.TrimSpace(string(netData))
if temp == "STOP" {
break
}
log.Println("Recieved: ", temp)
result := strconv.Itoa(random()) + "\n"
conn.Write([]byte(string(result)))
return
}
}

View File

@ -1,129 +0,0 @@
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync/atomic"
"time"
)
type key int
const (
requestIDKey key = 0
)
var (
listenAddr string
healthy int32
)
func main() {
flag.StringVar(&listenAddr, "listen-addr", ":5000", "server listen address")
flag.Parse()
logger := log.New(os.Stdout, "http: ", log.LstdFlags)
logger.Println("Server is starting...")
router := http.NewServeMux()
router.Handle("/", index())
router.Handle("/healthz", healthz())
nextRequestID := func() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
server := &http.Server{
Addr: listenAddr,
Handler: tracing(nextRequestID)(logging(logger)(router)),
ErrorLog: logger,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
done := make(chan bool)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
go func() {
<-quit
logger.Println("Server is shutting down...")
atomic.StoreInt32(&healthy, 0)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
logger.Fatalf("Could not gracefully shutdown the server: %v\n", err)
}
close(done)
}()
logger.Println("Server is ready to handle requests at", listenAddr)
atomic.StoreInt32(&healthy, 1)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("Could not listen on %s: %v\n", listenAddr, err)
}
<-done
logger.Println("Server stopped")
}
func index() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "Hello, World!")
})
}
func healthz() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if atomic.LoadInt32(&healthy) == 1 {
w.WriteHeader(http.StatusNoContent)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
})
}
func logging(logger *log.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
requestID, ok := r.Context().Value(requestIDKey).(string)
if !ok {
requestID = "unknown"
}
logger.Println(requestID, r.Method, r.URL.Path, r.RemoteAddr, r.UserAgent())
}()
next.ServeHTTP(w, r)
})
}
}
func tracing(nextRequestID func() string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get("X-Request-Id")
if requestID == "" {
requestID = nextRequestID()
}
ctx := context.WithValue(r.Context(), requestIDKey, requestID)
w.Header().Set("X-Request-Id", requestID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}

View File

@ -1,5 +0,0 @@
server:
go run server.go
client:
go run client.go

View File

@ -1,39 +0,0 @@
package main
import "fmt"
import "net"
import "os"
import "log"
import "strconv"
// import "io"
// import "encoding/csv"
// import "flag"
import "github.com/golang/protobuf/proto"
import pb "git.wit.com/wit/witProtobuf"
func main() {
msg := pb.CreateSampleEvent()
msg.Name = "from dnssecsockettest()"
data, err := proto.Marshal(msg)
if (err != nil) {
log.Printf("something fucked up happened")
}
sendDataToDest(data)
}
func sendDataToDest(data []byte) {
conn, err := net.Dial("tcp", "localhost:8080")
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
return
}
n, err := conn.Write(data)
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
return
}
fmt.Println("Sent " + strconv.Itoa(n) + " bytes")
}

View File

@ -1,58 +0,0 @@
package main
import "fmt"
import "log"
import "net"
import "os"
import "io"
import "bytes"
import "github.com/golang/protobuf/proto"
import pb "git.wit.com/wit/witProtobuf"
// sits on the channel waiting for Events
func processEvents(mychannel chan *pb.Event) {
for {
message := <-mychannel
log.Println("processEvents() on channel recieved a message")
pb.DumpEventData(message)
}
}
func main() {
fmt.Println("Staring Server..")
c := make(chan *pb.Event)
go processEvents(c)
listener, err := net.Listen("tcp", ":8080")
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
for {
if conn, err := listener.Accept(); err == nil {
go handleProtoClient(conn, c)
} else {
continue
}
}
}
func handleProtoClient(conn net.Conn, c chan *pb.Event) {
fmt.Println("Connected!")
defer conn.Close()
var buf bytes.Buffer
_, err := io.Copy(&buf, conn)
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
return
}
pdata := new(pb.Event)
err = proto.Unmarshal(buf.Bytes(), pdata)
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
return
}
c <- pdata
}

View File

@ -1,5 +0,0 @@
server:
go run server.go
client:
go run client.go

View File

@ -1,103 +0,0 @@
// Copyright 2015 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "flag"
import "log"
import "net/url"
import "os"
import "os/signal"
import "time"
import "github.com/gorilla/websocket"
import "github.com/golang/protobuf/proto"
import pb "git.wit.com/wit/witProtobuf"
var addr = flag.String("addr", "v000185.testing.com.customers.wprod.wit.com:9000", "http service address")
func readConn(done chan struct{}, conn *websocket.Conn) {
defer close(done)
for {
mytype, message, err := conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
if (mytype == websocket.BinaryMessage) {
pdata := new(pb.Event)
err = proto.Unmarshal(message, pdata)
if (err != nil) {
log.Printf("readConn() something fucked up happened in Unmarshal")
}
log.Printf("recv binary: %s", pdata)
} else {
log.Printf("recv: %s", message)
// log.Printf("type, err = ", mytype, err)
}
}
}
func main() {
flag.Parse()
log.SetFlags(0)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "ws", Host: *addr, Path: "/echo"}
log.Printf("connecting to %s", u.String())
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer conn.Close()
done := make(chan struct{})
// handle inbound messages on the channel
go readConn(done, conn)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-done:
return
case t := <-ticker.C:
err := conn.WriteMessage(websocket.TextMessage, []byte(t.String()))
if err != nil {
log.Println("write:", err)
return
}
msg := pb.CreateSampleEvent()
msg.Name = "test echo over gorilla websocket"
data, _ := proto.Marshal(msg)
err2 := conn.WriteMessage(websocket.BinaryMessage, data)
if err2 != nil {
log.Println("write:", err2)
return
}
case <-interrupt:
log.Println("interrupt")
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-done:
case <-time.After(time.Second):
}
return
}
}
}

View File

@ -1,268 +0,0 @@
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Command server is a test server for the Autobahn WebSockets Test Suite.
package main
import (
"errors"
"flag"
"io"
"log"
"net/http"
"time"
"unicode/utf8"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
EnableCompression: true,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// echoCopy echoes messages from the client using io.Copy.
func echoCopy(w http.ResponseWriter, r *http.Request, writerOnly bool) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade:", err)
return
}
defer conn.Close()
for {
mt, r, err := conn.NextReader()
if err != nil {
if err != io.EOF {
log.Println("NextReader:", err)
}
return
}
if mt == websocket.TextMessage {
r = &validator{r: r}
}
w, err := conn.NextWriter(mt)
if err != nil {
log.Println("NextWriter:", err)
return
}
if mt == websocket.TextMessage {
r = &validator{r: r}
}
if writerOnly {
_, err = io.Copy(struct{ io.Writer }{w}, r)
} else {
_, err = io.Copy(w, r)
}
if err != nil {
if err == errInvalidUTF8 {
conn.WriteControl(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseInvalidFramePayloadData, ""),
time.Time{})
}
log.Println("Copy:", err)
return
}
err = w.Close()
if err != nil {
log.Println("Close:", err)
return
}
}
}
func echoCopyWriterOnly(w http.ResponseWriter, r *http.Request) {
echoCopy(w, r, true)
}
func echoCopyFull(w http.ResponseWriter, r *http.Request) {
echoCopy(w, r, false)
}
// echoReadAll echoes messages from the client by reading the entire message
// with ioutil.ReadAll.
func echoReadAll(w http.ResponseWriter, r *http.Request, writeMessage, writePrepared bool) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade:", err)
return
}
defer conn.Close()
for {
mt, b, err := conn.ReadMessage()
if err != nil {
if err != io.EOF {
log.Println("NextReader:", err)
}
return
}
if mt == websocket.TextMessage {
if !utf8.Valid(b) {
conn.WriteControl(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseInvalidFramePayloadData, ""),
time.Time{})
log.Println("ReadAll: invalid utf8")
}
}
if writeMessage {
if !writePrepared {
err = conn.WriteMessage(mt, b)
if err != nil {
log.Println("WriteMessage:", err)
}
} else {
pm, err := websocket.NewPreparedMessage(mt, b)
if err != nil {
log.Println("NewPreparedMessage:", err)
return
}
err = conn.WritePreparedMessage(pm)
if err != nil {
log.Println("WritePreparedMessage:", err)
}
}
} else {
w, err := conn.NextWriter(mt)
if err != nil {
log.Println("NextWriter:", err)
return
}
if _, err := w.Write(b); err != nil {
log.Println("Writer:", err)
return
}
if err := w.Close(); err != nil {
log.Println("Close:", err)
return
}
}
}
}
func echoReadAllWriter(w http.ResponseWriter, r *http.Request) {
echoReadAll(w, r, false, false)
}
func echoReadAllWriteMessage(w http.ResponseWriter, r *http.Request) {
echoReadAll(w, r, true, false)
}
func echoReadAllWritePreparedMessage(w http.ResponseWriter, r *http.Request) {
echoReadAll(w, r, true, true)
}
func serveHome(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "Not found.", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, "<html><body>Echo Server</body></html>")
}
var addr = flag.String("addr", ":9000", "http service address")
func main() {
flag.Parse()
http.HandleFunc("/", serveHome)
http.HandleFunc("/c", echoCopyWriterOnly)
http.HandleFunc("/f", echoCopyFull)
http.HandleFunc("/echo", echoCopyFull)
http.HandleFunc("/r", echoReadAllWriter)
http.HandleFunc("/m", echoReadAllWriteMessage)
http.HandleFunc("/p", echoReadAllWritePreparedMessage)
log.Println("Starting http.ListenAndServe() on port 9000")
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
type validator struct {
state int
x rune
r io.Reader
}
var errInvalidUTF8 = errors.New("invalid utf8")
func (r *validator) Read(p []byte) (int, error) {
n, err := r.r.Read(p)
state := r.state
x := r.x
for _, b := range p[:n] {
state, x = decode(state, x, b)
if state == utf8Reject {
break
}
}
r.state = state
r.x = x
if state == utf8Reject || (err == io.EOF && state != utf8Accept) {
return n, errInvalidUTF8
}
return n, err
}
// UTF-8 decoder from http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
//
// Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
var utf8d = [...]byte{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1f
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20..3f
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5f
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7f
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9f
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // a0..bf
8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c0..df
0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // e0..ef
0xb, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // f0..ff
0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // s7..s8
}
const (
utf8Accept = 0
utf8Reject = 1
)
func decode(state int, x rune, b byte) (int, rune) {
t := utf8d[b]
if state != utf8Accept {
x = rune(b&0x3f) | (x << 6)
} else {
x = rune((0xff >> t) & b)
}
state = int(utf8d[256+state*16+int(t)])
return state, x
}

View File

@ -1,7 +0,0 @@
run:
go run *.go
gaper:
# 'gaper' is a simple and smart golang tool that just rebuilds every time you change a file
# go get -u github.com/maxcnunes/gaper
gaper

View File

@ -1,97 +0,0 @@
package main
import "log"
import "bytes"
import "os"
import "bufio"
// import "io"
import "io/ioutil"
import "github.com/golang/protobuf/proto"
import pb "git.wit.com/wit/witProtobuf"
// Main reads the entire address book from a file and prints all the
// information inside.
func main() {
TestWriteEvent()
in, err := ioutil.ReadFile("/tmp/testing4.protobuf")
if err != nil {
log.Fatalln("Error reading file:", err)
}
allEvents := &pb.Event{}
if err := proto.Unmarshal(in, allEvents); err != nil {
log.Fatalln("Failed to parse events:", err)
}
// listPeople(os.Stdout, allEvents)
// got := in.String()
log.Println(in)
}
func marshalWriteToFile(myWriter *bufio.Writer, e *pb.Event) {
buf, err := proto.Marshal(e)
if err != nil {
log.Fatal("marshaling error: ", err)
}
tmp, err := myWriter.Write(buf)
myWriter.Flush()
log.Println("bufio.Write() tmp, err = ", tmp, err)
e.Id += 1
e.Name = "jcarrEvent21"
buf, err = proto.Marshal(e)
tmp2, err := myWriter.Write(buf)
myWriter.Flush()
log.Println("bufio.Write() tmp2, err = ", tmp2, err)
}
func TestWriteEvent() {
buf := new(bytes.Buffer)
// [START populate_proto]
e := &pb.Event{
Id: 1234,
Name: "jcarrEvent1",
Email: "jcarr@wit.com",
Results: []*pb.Response{
{Name: "555-4321", Type: pb.EventType_ADD},
},
}
// [END populate_proto]
e.Id += 1
e.Name = "jcarrEvent2"
got := buf.String()
log.Println(got)
newfile, _ := os.Create("/tmp/testing4.protobuf")
myWriter := bufio.NewWriter(newfile)
marshalWriteToFile(myWriter, e)
marshalUnmarshal()
}
func marshalUnmarshal() {
test := &pb.Event{
Id: 1234,
Name: "jcarrEvent1",
Email: "jcarr@wit.com",
Result: []*pb.Response{
{Name: "555-4321", Type: pb.EventType_ADD},
},
}
data, err := proto.Marshal(test)
if err != nil {
log.Fatal("marshaling error: ", err)
}
newTest := &pb.Event{}
err = proto.Unmarshal(data, newTest)
if err != nil {
log.Fatal("unmarshaling error: ", err)
} else {
log.Println("proto.Marshal() and proto.Unmarshal() worked")
}
}

View File

@ -1,6 +0,0 @@
build:
go build
./goclient
run:
go run *.go

View File

@ -1,153 +0,0 @@
package main
import "log"
import "net/http"
import "io/ioutil"
import "github.com/tidwall/gjson"
import pb "git.wit.com/wit/witProtobuf"
func main() {
ge := pb.MakeOkResponse()
c := pb.MakeDefaultConfig()
ge.Account = c.Accounts[0]
ge.Account.Email = "jcarr@wit.com"
ge.Account.Password = "yowzayowza"
ge.Account.URL = "http://stackapi:4000/"
// ge.Account.Token = "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJ4IjozLCJyIjoiIiwiY3NyZiI6Ijloa0VYa2M0YURxTlVqSElGU2VJYUJoOCIsImV4cCI6MTU1OTY3OTAxMywiaXNzIjoid2l0Iiwic3ViIjoiamNhcnJAd2l0LmNvbSJ9.eCfhg3_VYBZh1ve69p0Op4U9L2T9CAZY5qPtx7vNswe3TB-y1nz2X2vw4AzPwfy4fACKeQGkJmWGedt1NKQP-WaZNVtGpgX9g0yvloCob2OMQwsLhq3X7e83weiK_Jlk"
ge.Account.Token = "badtoken"
junkEvent := processLoginEvent(ge)
log.Println("processAddEvent() junkEvent =", junkEvent)
log.Println("processAddEvent() START ge.Account =", ge.Account)
tmp := string(GETbody(ge, "http://stackapi:4000/me"))
log.Println("processAddEvent() recieved json=", tmp)
tmp = string(GETbody(ge, "http://stackapi:4000/clusters"))
log.Println("processAddEvent() recieved json=", tmp)
url := ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
newVM := string(POSTbody(ge, url))
log.Println("processAddEvent() recieved newVM=", newVM)
}
// check if a user can login here
// tries generates a new TOKEN if the old one doesn't work
// fails with the appropriate protobuf response to send
// to the GUI client
func processLoginEvent(ge *pb.Event) *pb.Event {
log.Println("processLoginEvent() LOGIN START")
if (checkLogin(ge) == false) {
e := pb.MakeFailResponse()
e.Comment = "LOGIN FAILED"
log.Println(e.Comment)
return e
}
e := pb.MakeOkResponse()
e.Account = ge.Account
e.Comment = "processLoginEvent() LOGIN WORKED"
log.Println(e.Comment)
return e
}
func checkLogin(ge *pb.Event) bool {
if (ge.Account == nil) {
log.Println("checkLogin() pb.Event.Account == nil ERROR")
return false
}
url := ge.Account.URL + "me"
json := string(GETbody(ge, url))
if (json != "") {
email := gjson.Get(json, "email")
log.Println("origLoginCheck() ", "email = ", email.String())
log.Println("origLoginCheck() ", "ge.Account.Username = ", ge.Account.Username)
if (email.String() == ge.Account.Username) {
ge.Comment = "origLoginCheck() THIS TOTALLY WORKED MAN"
}
log.Println(ge.Comment)
return true
}
log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)")
return updateToken(ge)
}
func updateToken(ge *pb.Event) bool {
if ge == nil {
return false
}
url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password
// json := newFetchBody(ge, "POST", url)
json := POSTbody(ge, url)
jwt := gjson.Get(json, "jwt")
if (jwt.String() == "") {
ge.Comment = "updateToken() GOT TOKEN == nil"
log.Println("updateToken() ge.Comment =", ge.Comment)
return false
}
if (jwt.String() != ge.Account.Token) {
log.Println("updateToken() GOT NEW TOKEN", jwt)
ge.Account.Token = jwt.String()
}
log.Println("updateToken() END")
return true
}
func GETbody(ge *pb.Event, URL string) string {
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
log.Println("GETbody() can't make new req")
return ""
}
req.Header.Set("X-Wit-Auth", ge.Account.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Can't exec the req to list networks: %s", err)
return ""
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
return ""
}
// log.Printf("GETbody() body =", string(body))
if body == nil {
log.Println("networks is nil")
return ""
}
return string(body)
}
func POSTbody(ge *pb.Event, URL string) string {
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
log.Println("POSTbody() url =", URL)
req, err := http.NewRequest("POST", URL, nil)
if err != nil {
log.Println("POSTbody() can't make new req")
return ""
}
req.Header.Set("X-Wit-Auth", ge.Account.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Can't exec the req to list networks: %s", err)
return ""
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
return ""
}
// log.Printf("GETbody() body =", string(body))
if body == nil {
log.Println("networks is nil")
return ""
}
return string(body)
}

View File

@ -1,6 +0,0 @@
build:
go build
./gorilla-server
run:
go run *.go

View File

@ -1,136 +0,0 @@
package main
import "log"
import "net/http"
import "io/ioutil"
import "github.com/tidwall/gjson"
import pb "git.wit.com/wit/witProtobuf"
// check if a user can login here
// tries generates a new TOKEN if the old one doesn't work
// fails with the appropriate protobuf response to send
// to the GUI client
func processLoginEvent(ge *pb.Event) *pb.Event {
log.Println("processLoginEvent() LOGIN START")
if (checkLogin(ge) == false) {
e := pb.MakeFailResponse()
e.Comment = "LOGIN FAILED"
log.Println(e.Comment)
return e
}
e := pb.MakeOkResponse()
e.Account = ge.Account
e.Comment = "processLoginEvent() LOGIN WORKED"
log.Println(e.Comment)
return e
}
func checkLogin(ge *pb.Event) bool {
if (ge.Account == nil) {
log.Println("checkLogin() pb.Event.Account == nil ERROR")
return false
}
url := ge.Account.URL + "me"
json := string(GETbody(ge, url))
if (json != "") {
email := gjson.Get(json, "email")
log.Println("origLoginCheck() ", "email = ", email.String())
log.Println("origLoginCheck() ", "ge.Account.Username = ", ge.Account.Username)
if (email.String() == ge.Account.Username) {
ge.Comment = "origLoginCheck() THIS TOTALLY WORKED MAN"
}
log.Println(ge.Comment)
return true
}
log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)")
return updateToken(ge)
}
func updateToken(ge *pb.Event) bool {
if ge == nil {
return false
}
url := ge.Account.URL + "auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password
// json := newFetchBody(ge, "POST", url)
json := POSTbody(ge, url)
jwt := gjson.Get(json, "jwt")
if (jwt.String() == "") {
ge.Comment = "updateToken() GOT TOKEN == nil"
log.Println("updateToken() ge.Comment =", ge.Comment)
return false
}
if (jwt.String() != ge.Account.Token) {
log.Println("updateToken() GOT NEW TOKEN", jwt)
ge.Account.Token = jwt.String()
}
log.Println("updateToken() END")
return true
}
func GETbody(ge *pb.Event, URL string) string {
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
log.Println("GETbody() can't make new req")
return ""
}
req.Header.Set("X-Wit-Auth", ge.Account.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Can't exec the req to list networks: %s", err)
return ""
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
return ""
}
// log.Printf("GETbody() body =", string(body))
if body == nil {
log.Println("networks is nil")
return ""
}
return string(body)
}
func POSTbody(ge *pb.Event, URL string) string {
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
log.Println("POSTbody() url =", URL)
req, err := http.NewRequest("POST", URL, nil)
if err != nil {
log.Println("POSTbody() can't make new req")
return ""
}
token := ge.Account.Token
if (token == "") {
token = "POSTbodyEmptyToken"
}
log.Println("POSTbody() ge.Account.Token =", token)
req.Header.Set("X-Wit-Auth", ge.Account.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("POSTbody() Can't exec the req to list networks: %s", err)
return ""
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("POSTbody() can't read resp")
return ""
}
// log.Printf("GETbody() body =", string(body))
if body == nil {
log.Println("POSTbody() body is nil")
return ""
}
log.Println("POSTbody() END body =", string(body))
return string(body)
}

View File

@ -1,218 +0,0 @@
package main
import "flag"
import "log"
import "fmt"
import "regexp"
import "net/http"
import "encoding/json"
import "io/ioutil"
import pb "git.wit.com/wit/witProtobuf"
import "github.com/davecgh/go-spew/spew"
import "github.com/tidwall/gjson"
type network struct {
Subdomain string `json:"subdomain"`
}
func fetchNetworks(ge *pb.Event) []network {
var networks []network
req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
if err != nil { log.Fatal("can't make new req") }
req.Header.Set("X-Wit-Auth", ge.Account.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil { log.Printf("Can't exec the req to list networks: %s", err) }
body, err := ioutil.ReadAll(resp.Body)
if err != nil { log.Fatal("can't read resp") }
log.Printf("NETWORKS =", string(body))
if body == nil {
log.Println("networks is nil")
} else {
len := len(body)
err = json.Unmarshal(body, &networks)
if err != nil {
log.Println("body = ", body)
log.Println("body len = ", len)
log.Println("can't unmarshal resp for list networks: %s", err)
}
}
spew.Dump(networks)
return networks
}
/*
{
"name": "vm10",
"cpu": 4,
"ram": 2048,
"created_on": "2019-03-18T19:22:10.755428Z",
"scope": "private",
"role": "k8s-node",
"stage": "Creating...",
"seconds_left": 300,
"base_image": "debcore_guest_latest",
"address_list": [
"192.168.0.1/32"
]
},
*/
type address struct {
Address string
}
type vm struct {
Name string `json:"name"`
Role string `json:"role"`
BaseImage string `json:"base_image"`
}
func fetchVMs(ge *pb.Event, network string) []vm {
var vms []vm
req, err := http.NewRequest("GET", ge.Account.URL + "vms/" + network, nil)
if err != nil {
log.Println("fetchVMs() can't make new req")
return vms
}
req.Header.Set("X-Wit-Auth", ge.Account.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("fetchVMs() Can't exec the req to fetch vms for %s: %s", network, err)
return vms
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("fetchVMs() can't read resp")
return vms
}
err = json.Unmarshal(body, &vms)
if err != nil {
log.Println("fetchVMs() can't unmarshal resp for fetch vms: ", err, "\n\n", network, string(body))
return vms
}
spew.Dump(vms)
return vms
}
var addr = flag.String("addr", ":9000", "http service address")
func main() {
flag.Parse()
http.HandleFunc("/", serveHome)
http.HandleFunc("/event", eventHandler)
log.Println("Starting http.ListenAndServe() on port 9000")
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// Gets a list of all the customer VM's
func processGetEvent(ge *pb.Event) *pb.Event {
log.Println("processGetEvent() START")
if (checkLogin(ge) == false) {
e := pb.MakeFailResponse()
e.Comment = "processGetEvent() LOGIN FAILED on checkLogin(ge)"
log.Println(e.Comment)
return e
}
log.Println("processGetEvent() ge.Account.Token =", ge.Account.Token)
log.Println("processGetEvent() ge.Account.Username =", ge.Account.Username)
log.Println("processGetEvent() ge.Account.Email =", ge.Account.Email)
e := pb.Event{}
networks := fetchNetworks(ge)
for _, network := range(networks) {
log.Println("network.Subdomain =",network.Subdomain)
log.Println("network=",network)
enetwork := pb.Event_Network{}
enetwork.Name = network.Subdomain
e.Networks = append(e.Networks, &enetwork)
vms := fetchVMs(ge, network.Subdomain)
for _, vm := range(vms) {
evm := pb.Event_VM{}
evm.Name = vm.Name
evm.Cpus = 1
evm.Memory = 512
evm.Disk = 20
// TODO: bmath -- let's change the stack-api to remove this hack
evm.Hostname = vm.Name + "." + enetwork.Name + ".customers.wprod.wit.com"
e.Vms = append(e.Vms, &evm)
log.Println("\tvm.Name =",vm.Name)
log.Println("\tvm =",vm)
}
}
return &e
}
/*
// should check a user can login here
func processLoginEvent(ge *pb.Event) *pb.Event {
log.Println("processLoginEvent() LOGIN START")
if (checkLogin(ge) == false) {
e := pb.MakeFailResponse()
e.Comment = "LOGIN FAILED"
log.Println(e.Comment)
return e
}
e := pb.MakeOkResponse()
e.Account = ge.Account
e.Comment = "processLoginEvent() LOGIN WORKED"
log.Println(e.Comment)
return e
}
*/
func JSONtoVM(ge *pb.Event, json string) {
tmp := "network_addr"
value := gjson.Get(json, tmp)
println(tmp, "=", value.String())
tmp = "public_mac"
value = gjson.Get(json, tmp)
println(tmp, "=", value.String())
tmp = "public_addr"
value = gjson.Get(json, tmp)
println(tmp, "=", value.String())
}
func processAddEvent(ge *pb.Event) *pb.Event {
log.Println("processAddEvent() START ge.Account =", ge.Account)
if (checkLogin(ge) == false) {
e := pb.MakeFailResponse()
e.Comment = "processAddEvent() LOGIN FAILED. ge.Account = nil"
log.Println(e.Comment)
return e
}
a := regexp.MustCompile(`\.`) // split subdomain on .
stuff := a.Split(ge.Account.Domain, -1)
log.Printf("processAddEvent() stuff = %q\n", stuff)
log.Println("processAddEvent() host =", stuff[0])
log.Println("processAddEvent() memory =", ge.Vms[0].Memory)
memory := fmt.Sprintf("%d", ge.Vms[0].Memory)
url := ge.Account.URL + "vms/" + stuff[0] + "?count=1&cpu=2&ram=" + memory + "&disk=25"
log.Println("processAddEvent() new URL =", url)
newVM := string(POSTbody(ge, url))
log.Println("processAddEvent() recieved newVM=", newVM)
e := pb.MakeOkResponse()
e.Account = ge.Account
e.Comment = newVM
log.Println(e.Comment)
return e
}

View File

@ -1,111 +0,0 @@
package main
// These routines just recieve protobufs and pass protobufs back over
// a websocket (in this current case, a 'gorilla/websocket'
//
// These routines should probably be kept pretty generic in that regard
import "io"
import "log"
import "net/http"
import "github.com/golang/protobuf/proto"
import pb "git.wit.com/wit/witProtobuf"
import "github.com/gorilla/websocket"
// Also potentially look at:
// https://www.reddit.com/r/golang/comments/boznnz/an_alternative_to_gorilla_sessions_jeff_manage/
var upgrader = websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
EnableCompression: true,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func sendProtobuf(conn *websocket.Conn, msg *pb.Event) {
log.Println("sendProtobuf() START")
data, _ := proto.Marshal(msg)
err := conn.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
log.Println("sendProtobuf() write:", err)
}
log.Println("sendProtobuf() END")
}
func eventHandler(w http.ResponseWriter, r *http.Request) {
log.Println("echoHandler() START")
log.Println("echoHandler() ATTEMPT r.Header.Get", r.Header.Get("User-Agent"))
log.Println("echoHandler() ATTEMPT r.Header", r.Header)
for key, value := range r.Header {
log.Println("echoHandler() HEADER key, value =", key, value)
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade:", err)
return
}
defer conn.Close()
for {
mytype, message, err := conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
if mytype == websocket.TextMessage {
// probably this should never happen since the client socket connection should have
// already been dropped. If a user is doing this, the account should probably be
// terminated as abuse
log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. NO HANDLER FOR THIS YET)")
log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. THIS SHOULD NEVER HAPPEN)")
log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. SUSPEND THIS USER)")
}
if mytype == websocket.BinaryMessage {
log.Println("\teventHandler() GOT websocket.BinaryMessage")
pdata := new(pb.Event)
var e *pb.Event
e = new(pb.Event)
err = proto.Unmarshal(message, pdata)
if (err != nil) {
log.Printf("readConn() something fucked up happened in Unmarshal")
}
log.Printf("\teventHandler() pdata.Type =", pdata.Type)
log.Printf("\teventHandler() recv binary: %s", pdata)
if pdata.Type == pb.Event_GET {
log.Printf("\teventHandler() DOING Event_GET")
e = processGetEvent(pdata)
} else if pdata.Type == pb.Event_LOGIN {
log.Printf("\teventHandler() DOING Event_LOGIN")
e = processLoginEvent(pdata)
} else if pdata.Type == pb.Event_ADD {
log.Printf("\teventHandler() DOING Event_ADD")
e = processAddEvent(pdata)
}
if (e == nil) {
e = pb.MakeFailResponse()
e.Comment = "websocket.BinaryMessage LOGIN FAILED"
}
e.Account = pdata.Account
log.Println("\teventHandler() e.Comment =", e.Comment)
sendProtobuf(conn, e)
}
}
}
func serveHome(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "Not found.", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, "<html><body>Echo Server</body></html>")
}

30
log.go
View File

@ -1,30 +0,0 @@
package dnssecsocket
import (
witlog "git.wit.org/wit/gui/log"
)
// various debugging flags
var logNow bool = true // useful for active development
var logError bool = true
var logWarn bool = false
var logInfo bool = false
var logVerbose bool = false
var SPEW witlog.Spewt
// var log interface{}
func log(a ...any) {
witlog.Where = "wit/gui"
witlog.Log(a...)
}
func sleep(a ...any) {
witlog.Sleep(a...)
}
func exit(a ...any) {
log(logError, "got to log() exit")
witlog.Exit(a...)
}

View File

@ -31,17 +31,7 @@ const (
CONN_TYPE = "tcp"
)
/*
go func(c net.Conn) {
defer c.Close()
io.Copy(os.Stdout, c)
}(conn)
*/
func main() {
// redirect all this output to systemd
dnssecsocket.UseJournalctl()
hostname := fqdn.Get()
log.Println("FQDN hostname is", hostname)
@ -54,10 +44,8 @@ func main() {
listenstr := "[" + ipaddr + "]:" + CONN_PORT
log.Println("listenstr", listenstr)
myTCPAddr, err := net.ResolveTCPAddr("tcp", listenstr)
// // Listen for incoming connections on the IPv6 address only
l, err := net.ListenTCP("tcp", myTCPAddr)
l, err := net.Listen(CONN_TYPE, listenstr)
if err != nil {
log.Println("Error listening:", err.Error())
return
@ -72,14 +60,12 @@ func main() {
for {
// Listen for an incoming connection.
conn, err := l.AcceptTCP()
conn, err := l.Accept()
if err != nil {
log.Println("Error accepting: ", err.Error())
return
}
log.Println("Accepted new connection from: " + conn.RemoteAddr().String())
// Handle connections in a new goroutine.
go dnssecsocket.HandleConnection(conn)
}