Compare commits

...

60 Commits

Author SHA1 Message Date
Jeff Carr 96c44afee1 fix hostname with domain name and vis-a-vis
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-06-04 23:15:55 +00:00
Jeff Carr 0ef6a9b47b make token incase it is empty
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-06-04 04:44:50 +00:00
Jeff Carr 00d19a95da move gorilla websocket functions into their own file
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-29 08:55:41 +00:00
Jeff Carr 5357cbd7db never die in fetchVMs(). Use the domain from the config file
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-29 01:14:07 +00:00
Jeff Carr 7c5b906f5f ok. ADD works from the GUI
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-28 20:28:25 +00:00
Jeff Carr 1cb13782e2 fixed client code example.
WTF was wrong with the other code I made?

Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-28 20:19:32 +00:00
Jeff Carr 34351697e3 working POSTbody()
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-28 20:02:05 +00:00
Jeff Carr 2b7a5fa43d broken still
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-28 19:49:51 +00:00
Jeff Carr 7883c0d16d add a simple client to test ADD
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-28 19:35:42 +00:00
Jeff Carr 12ef7e0dcf more work on converting the JSON
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-28 06:18:44 +00:00
Jeff Carr da12ea4087 start to clean code for interaction with protobuf structures
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-28 04:29:55 +00:00
Jeff Carr 82c4ac9c01 more debugging
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-26 20:20:50 +00:00
Jeff Carr 88c3ef5992 better debugging
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-26 20:13:50 +00:00
Jeff Carr 936b2d6c91 more code cleanups
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-26 19:35:13 +00:00
Jeff Carr 1ef3c8f2dd fail if account was not sent
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-26 17:53:02 +00:00
Jeff Carr 2d3a0a900a lots of cleanups to token handling. protobuf fixes
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-26 08:04:12 +00:00
Jeff Carr b16b4c84bb cleaning up the code further
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-26 07:27:51 +00:00
Jeff Carr deac243443 correctly request a new token
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-26 06:22:37 +00:00
Jeff Carr 53c9620b8a write to files
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-25 17:11:32 -07:00
Jeff Carr fb698301e4 convert JSON to protobuf
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-25 14:24:07 -07:00
Jeff Carr 95b4b77b27 final small version
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-25 14:09:59 -07:00
Jeff Carr 91e02c329d dump the JSON body
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-25 14:09:02 -07:00
Jeff Carr 7110856b18 allow 'OPTIONS' aka 'CORS' on http sockets
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-25 13:10:25 -07:00
Jeff Carr 58b80b499a attempt 3
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-25 12:30:07 -07:00
Jeff Carr 42ef854233 even smaller
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-25 12:17:07 -07:00
Jeff Carr 5ace7b049f misc other tests
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-25 12:03:11 -07:00
Jeff Carr 280e8218ae another example
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-25 11:37:47 -07:00
Jeff Carr c19043cd52 simple daemon to listen to http requests
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-25 11:17:03 -07:00
Jeff Carr c32f8585fa disable journalhook because it doesn't build on Windows
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-24 21:41:19 -07:00
Jeff Carr 13af6a6273 start a unified json function
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-24 22:36:56 +00:00
Jeff Carr 19147ea950 respond to login attempt
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-24 06:43:42 +00:00
Jeff Carr 0e7cb059d4 an attempt at verifing the login token
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-24 05:46:37 +00:00
Jeff Carr 7274c5c3c3 merge into a single file to make it easier to work with
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-24 03:46:02 +00:00
Jeff Carr e4031a68c0 token is in GUI
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-24 02:52:37 +00:00
Jeff Carr 47a5b0659c recieve the token
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-24 02:33:19 +00:00
Jeff Carr bb12949637 update to new token
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-23 03:09:51 +00:00
Jeff Carr 0ee9a0e661 maybe an alternative to look at?
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-15 20:28:59 +00:00
Jeff Carr 6ce08105ad fix error on broken API or JSON networks being nil
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-14 02:21:28 +00:00
Jeff Carr 7bcbe607a0 convert the JSON into a google protobuf
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-12 19:19:15 +00:00
Jeff Carr 0b6d5fbac9 send back a protobuf
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-12 07:28:07 +00:00
Jeff Carr 1bfaaa331d ready to attempt returning a protobuf
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-12 06:07:22 +00:00
Jeff Carr 0ad98fe594 remove all the extra stuff
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-12 05:57:19 +00:00
Jeff Carr f2068edf09 start a daemon to talk to the cloud-control-panel
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-12 05:48:59 +00:00
Jeff Carr cffb4344d0 s/c/conn/
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-11 20:14:37 -07:00
Jeff Carr 36a23911a8 successfully sent a google protobuf over a gorilla websocket in
a marshal'd binary form. echo protobuf works.

Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-11 17:22:14 -07:00
Jeff Carr 46ee8a456f make a seperate function to handle reading messages from the channel
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-11 17:07:28 -07:00
Jeff Carr 65f0d84f5b a working echo client and server using gorilla
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-11 16:59:13 -07:00
Jeff Carr 0a5aa8e447 use a standard Event Dump() function
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-11 13:20:24 -07:00
Jeff Carr f6018c0960 update the examples
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-11 12:59:46 -07:00
Jeff Carr bf5ff90e5b add debugging
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-11 03:50:38 -07:00
Jeff Carr b91b431ec6 add a working protobuf channel example
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-11 03:48:13 -07:00
Jeff Carr c34dc5f401 code runs. Writes out marshal'ed entries to a file, but is that anything other than garbage
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-10 17:41:58 -07:00
Jeff Carr 1c974ed95e successfully marshal a protobuf witProtobuf.Event and write it to a file
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-10 14:57:33 -07:00
Jeff Carr 5882fa20ac A first for me with google protobuf's
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-10 12:36:36 -07:00
Jeff Carr e476e50d48 avoid a potential crash
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-10 03:42:48 +00:00
Jeff Carr d6f15e9e22 allow dns lookup output to be sent to systemctl
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-10 03:22:44 +00:00
Jeff Carr 71f1aa0cfc moved shell to the 'wit' account
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-10 03:08:22 +00:00
Jeff Carr d38dbbdf70 add localTCPAddr := dnssecsocket.ResolveIPv6hostname("librem15.lab.wit.com")
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-09 17:57:51 -07:00
Jeff Carr 9070d87fba move logging to journalctl + start trying to debug missing bytes
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-10 00:24:52 +00:00
Jeff Carr 17e5b73aa4 convert to ListenTCP
Signed-off-by: Jeff Carr <jcarr@wit.com>
2019-05-09 13:20:57 +00:00
23 changed files with 1924 additions and 31 deletions

5
.gitignore vendored
View File

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

View File

@ -5,16 +5,16 @@
package dnssecsocket
// import "fmt"
// import "os"
import "os"
import "bufio"
import "math/rand"
import "net"
import "strconv"
import "strings"
// import "time"
import "log"
import log "github.com/sirupsen/logrus"
// import "github.com/wercker/journalhook"
import "git.wit.com/wit/shell"
// 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(c net.Conn) string {
clientAddr := c.RemoteAddr().String()
func GetRemoteAddr(conn net.TCPConn) string {
clientAddr := conn.RemoteAddr().String()
parts := strings.Split(clientAddr, "]")
ipv6 := parts[0]
return ipv6[1:]
@ -42,12 +42,22 @@ func GetRemoteAddr(c net.Conn) string {
// Each client must send it's hostname as the first line
// Then each hostname is verified with DNSSEC
//
func HandleConnection(c net.Conn) {
ipv6client := GetRemoteAddr(c)
func HandleConnection(conn *net.TCPConn) {
// Disable journalhook until it builds on Windows
// journalhook.Enable()
// spew.Dump(conn)
// ipv6client := GetRemoteAddr(c)
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 := bufio.NewReader(c).ReadString('\n')
netData, err := newreader.ReadString('\n')
if err != nil {
log.Println(err)
return
@ -56,21 +66,40 @@ func HandleConnection(c net.Conn) {
log.Println("Recieved client hostname as:", clientHostname)
dnsRR := Dnstrace(clientHostname, "AAAA")
if (dnsRR == nil) {
log.Println("dnsRR IS NIL")
log.Println("dnsRR IS NIL")
log.Println("dnsRR IS NIL")
conn.Close()
return
}
ipaddr := dns.Field(dnsRR, 1)
log.Println("Client claims to be: ", ipaddr)
log.Println("Serving to IPv6 client:", ipv6client)
/* TODO: figure out how to fix this check
if (ipaddr != ipv6client) {
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()
conn.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 {
netData, err := bufio.NewReader(c).ReadString('\n')
defer shell.SetStdout(os.Stdout)
defer conn.Close()
netData, err := newreader.ReadString('\n')
if err != nil {
log.Println(err)
return
@ -82,8 +111,21 @@ func HandleConnection(c net.Conn) {
}
log.Println("Recieved: ", temp)
if (temp == "list") {
log.Println("Should run list here")
shell.SetStdout(f)
shell.Run("/root/bin/list.testing.com")
shell.SetStdout(os.Stdout)
}
if (temp == "cpuinfo") {
log.Println("Should cat /proc/cpuinfo")
shell.SetStdout(f)
shell.Run("cat /proc/cpuinfo")
shell.SetStdout(os.Stdout)
}
result := strconv.Itoa(random()) + "\n"
c.Write([]byte(string(result)))
conn.Write([]byte(string(result)))
}
c.Close()
}

View File

@ -3,7 +3,6 @@ package dnssecsocket
// inspired from github.com/rs/dnstrace/main.go
import "fmt"
import "log"
import "net"
import "os"
import "strings"
@ -12,6 +11,11 @@ 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 (
@ -71,13 +75,13 @@ func Dnstrace(hostname string, qtypestr string) dns.RR {
qname := m.Question[0].Name
qtype := dns.TypeToString[m.Question[0].Qtype]
if i > 1 {
fmt.Println()
log.Println()
}
fmt.Printf("%d - query %s %s", i, qtype, qname)
log.Printf("%d - query %s %s", i, qtype, qname)
if r != nil {
fmt.Printf(": %s", strings.Replace(strings.Replace(r.MsgHdr.String(), ";; ", "", -1), "\n", ", ", -1))
log.Printf(": %s", strings.Replace(strings.Replace(r.MsgHdr.String(), ";; ", "", -1), "\n", ", ", -1))
}
fmt.Println()
log.Println()
for _, pr := range rs {
ln := 0
if pr.Msg != nil {
@ -90,15 +94,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))
}
fmt.Printf(col(" - %d bytes in %.2fms + %s lookup on %s(%s)", cDarkGray), ln, rtt, lrtt, pr.Server.Name, pr.Addr)
log.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
}
fmt.Printf(": %v", col(err, cRed))
log.Printf(": %v", col(err, cRed))
}
fmt.Print("\n")
log.Print("\n")
}
switch rtype {
@ -118,26 +122,26 @@ func Dnstrace(hostname string, qtypestr string) dns.RR {
} else {
glue = col("no glue", cYellow)
}
fmt.Printf("%s %d NS %s (%s)\n", label, s.TTL, s.Name, glue)
log.Printf("%s %d NS %s (%s)\n", label, s.TTL, s.Name, glue)
}
case client.ResponseTypeCNAME:
for _, rr := range r.Answer {
fmt.Println(rr)
log.Println(rr)
}
}
},
FollowingCNAME: func(domain, target string) {
fmt.Printf(col("\n~ following CNAME %s -> %s\n", cBlue), domain, target)
log.Printf(col("\n~ following CNAME %s -> %s\n", cBlue), domain, target)
},
}
r, rtt, err := c.RecursiveQuery(m, t)
if err != nil {
fmt.Printf(col("*** error: %v\n", cRed), err)
log.Printf(col("*** error: %v\n", cRed), err)
os.Exit(1)
}
fmt.Println()
fmt.Printf(col(";; Cold best path time: %s\n\n", cGray), rtt)
log.Println()
log.Printf(col(";; Cold best path time: %s\n\n", cGray), rtt)
for _, rr := range r.Answer {
log.Println(rr)
}
@ -146,3 +150,15 @@ func Dnstrace(hostname string, qtypestr string) dns.RR {
}
return nil
}
func ResolveIPv6hostname(hostname string) *net.TCPAddr {
dnsRR := Dnstrace(hostname, "AAAA")
aaaa := dns.Field(dnsRR, 1)
localTCPAddr, _ := net.ResolveTCPAddr("tcp", aaaa)
return localTCPAddr
}
func UseJournalctl() {
log.Println("journalhook is disabled because it breaks the Windows build right now")
// journalhook.Enable()
}

13
emaild/Makefile Normal file
View File

@ -0,0 +1,13 @@
# 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

76
emaild/alfonso.go Normal file
View File

@ -0,0 +1,76 @@
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")
}

268
emaild/main.go Normal file
View File

@ -0,0 +1,268 @@
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)
}
}

118
emaild/server.go Normal file
View File

@ -0,0 +1,118 @@
// 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
}
}

129
emaild/test2.go Normal file
View File

@ -0,0 +1,129 @@
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

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

View File

@ -0,0 +1,39 @@
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

@ -0,0 +1,58 @@
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
}

5
example-gorilla/Makefile Normal file
View File

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

103
example-gorilla/client.go Normal file
View File

@ -0,0 +1,103 @@
// 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
}
}
}

268
example-gorilla/server.go Normal file
View File

@ -0,0 +1,268 @@
// 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

@ -0,0 +1,7 @@
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

97
example-protobuf/main.go Normal file
View File

@ -0,0 +1,97 @@
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")
}
}

6
goclient/Makefile Normal file
View File

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

153
goclient/client.go Normal file
View File

@ -0,0 +1,153 @@
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)
}

6
gorilla-server/Makefile Normal file
View File

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

136
gorilla-server/apiJSON.go Normal file
View File

@ -0,0 +1,136 @@
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)
}

218
gorilla-server/main.go Normal file
View File

@ -0,0 +1,218 @@
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
}

111
gorilla-server/pbsocket.go Normal file
View File

@ -0,0 +1,111 @@
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>")
}

View File

@ -31,7 +31,17 @@ 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)
@ -44,8 +54,10 @@ 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.Listen(CONN_TYPE, listenstr)
l, err := net.ListenTCP("tcp", myTCPAddr)
if err != nil {
log.Println("Error listening:", err.Error())
return
@ -60,12 +72,14 @@ func main() {
for {
// Listen for an incoming connection.
conn, err := l.Accept()
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 dnssecsocket.HandleConnection(conn)
}