main.go 26.8 KB
Newer Older
1 2 3
package main

import (
4
	"bufio"
owlo's avatar
owlo committed
5
	"crypto/tls"
owlo's avatar
owlo committed
6
	"encoding/json"
7
	"fmt"
8
	"io"
9
	"io/ioutil"
owlo's avatar
owlo committed
10
	"log"
owlo's avatar
owlo committed
11
	"math/rand"
Localtoast's avatar
Localtoast committed
12
	"net/http"
Localtoast's avatar
Localtoast committed
13
	"os"
owlo's avatar
owlo committed
14
	"os/exec"
15
	"strconv"
owlo's avatar
owlo committed
16
	"strings"
owlo's avatar
owlo committed
17
	"time"
Localtoast's avatar
Localtoast committed
18

owlo's avatar
owlo committed
19
	flour "gitlab.com/localtoast/flourPower"
Localtoast's avatar
Localtoast committed
20
	"gitlab.com/owlo/Ogopogo/libs"
21 22
)

owlo's avatar
owlo committed
23 24
var hostname string

owlo's avatar
owlo committed
25
func validateLogin(signature string, file []byte, actors []libs.ActorS) bool {
owlo's avatar
owlo committed
26

owlo's avatar
owlo committed
27
	if signature != "" {
28 29 30 31 32 33 34
		bufferedReader, err := os.Create("tests/florp")

		if err != nil {
			fmt.Println("Error creating outfile")
		}
		bufferedReader.Write(file)
		bufferedReader.Close()
owlo's avatar
owlo committed
35
		decrypter := exec.Command("/bin/sh", "-c", "echo crashandburn | gpg --passphrase-fd 0 -o florpfile florp")
36 37
		decrypter.Dir = "tests/"
		decrypter.Run()
owlo's avatar
owlo committed
38
		defer os.Remove("tests/florp")
owlo's avatar
owlo committed
39
		florpFile, err := os.Open("tests/florpfile")
owlo's avatar
owlo committed
40
		defer os.Remove("tests/florpfile")
41 42 43 44
		florpReader := io.Reader(florpFile)
		florpScanner := bufio.NewScanner(florpReader)

		for florpScanner.Scan() {
owlo's avatar
owlo committed
45
			//fmt.Println(florpScanner.Text())
owlo's avatar
owlo committed
46
			if florpScanner.Text() == signature {
owlo's avatar
owlo committed
47
				florptext := strings.Split(florpScanner.Text(), ":")
owlo's avatar
owlo committed
48 49 50
				for i := range actors {
					if florptext[0] == actors[i].PreferredUsername {
						if florptext[1] == actors[i].Password {
owlo's avatar
owlo committed
51 52 53

							//Uncomment the line to clone the user at login
							//createUser(florptext[0], florptext[1])
owlo's avatar
owlo committed
54 55 56 57 58
							fmt.Println("LOGGED THE HECK IN")
							return true
						}
					}
				}
owlo's avatar
owlo committed
59
			}
60 61 62 63
		}

		if err != nil {
			fmt.Println("\033[2;255;0;0mERROR\033[0m")
owlo's avatar
owlo committed
64 65
			fmt.Println(err)
			return false
66
		}
owlo's avatar
owlo committed
67

68
	}
owlo's avatar
owlo committed
69

owlo's avatar
owlo committed
70 71
	return false
}
owlo's avatar
owlo committed
72
func createMobile(username string) bool {
73
	//AVATARCOMMAND
owlo's avatar
owlo committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	hostname = "http://snowcrash.network"
	UID := UIDMaker("user")
	userFile, err := os.Create("tests/mobiles/" + UID)
	if err != nil {
		fmt.Println("Error creating userfile")
	}
	var userActor libs.MobileS
	userActor.PreferredUsername = username
	userActor.Str = 15
	userActor.Dex = 15
	userActor.Int = 15
	userActor.Wis = 15
	userActor.Con = 15
	userActor.Tec = 1
	userActor.Inventory = map[int]string{
		0: "A mobile sticker",
	}
	userActor.Equipment = map[string]string{
		"body": "Fur",
	}
	userActor.Inbox = hostname + "#inbox/" + username
	userActor.Outbox = hostname + "#outbox/" + username
	userActor.Following = hostname + "#following/" + username
	userActor.Followers = hostname + "#followers/" + username
	userData, err := json.Marshal(&userActor)
	if err != nil {
		fmt.Println("Error marshalling json.")
		return false
	}
	userFile.Write(userData)
	userFile.Sync()
	return true
owlo's avatar
owlo committed
106

owlo's avatar
owlo committed
107
}
owlo's avatar
owlo committed
108 109 110 111 112 113 114 115 116 117 118 119 120
func createUser(username string, password string) bool {
	hostname = "http://snowcrash.network"
	UID := UIDMaker("user")
	userFile, err := os.Create("tests/" + UID)
	if err != nil {
		fmt.Println("Error creating userfile")
	}
	var userActor libs.ActorS
	userActor.PreferredUsername = username
	userActor.Password = password
	for i := 0; i < 32; i++ {
		fmt.Println("")
	}
owlo's avatar
owlo committed
121 122 123 124 125 126
	userActor.Str = 15
	userActor.Dex = 15
	userActor.Int = 15
	userActor.Wis = 15
	userActor.Con = 15
	userActor.Tec = 1
owlo's avatar
owlo committed
127 128
	userActor.Inventory = map[int]string{
		0: "A glowing disc.",
owlo's avatar
owlo committed
129 130
	}
	userActor.Equipment = map[string]string{
owlo's avatar
owlo committed
131
		"body": "A long flowing rainbow gown.",
owlo's avatar
owlo committed
132
	}
owlo's avatar
owlo committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146
	userActor.Inbox = hostname + "#inbox/" + username
	userActor.Outbox = hostname + "#outbox/" + username
	userActor.Following = hostname + "#following/" + username
	userActor.Followers = hostname + "#followers/" + username
	userData, err := json.Marshal(&userActor)
	if err != nil {
		fmt.Println("Error marshalling json.")
		return false
	}
	userFile.Write(userData)
	userFile.Sync()
	return true

}
owlo's avatar
owlo committed
147 148
func validate(signature string, file []byte) bool {

owlo's avatar
owlo committed
149
	bufferedReader, err := os.Create("tests/outfile")
150

owlo's avatar
owlo committed
151 152 153 154 155 156 157 158
	if err != nil {
		fmt.Println("Error creating outfile")
	}
	bufferedReader.Write(file)
	bufferedReader.Close()
	decrypter := exec.Command("/bin/sh", "-c", "echo crashandburn | gpg --passphrase-fd 0 -o out.json outfile")
	decrypter.Dir = "tests/"
	decrypter.Run()
159 160
	defer os.Remove("tests/out.json")
	UIDFile := "tests/out.json"
owlo's avatar
owlo committed
161
	UIDed := updateBork(UIDFile)
162
	UIDData, err := ioutil.ReadFile(UIDFile)
163 164 165
	if err != nil {
		fmt.Println("\033[2;255;0;0mERROR\033[0m")
	}
owlo's avatar
owlo committed
166
	//fmt.Println(UIDed.Id)
167
	UIDWriter, err := os.Create("tests/" + string(UIDed.Id))
owlo's avatar
owlo committed
168

169 170
	UIDWriter.Write(UIDData)
	UIDWriter.Close()
owlo's avatar
owlo committed
171 172 173 174

	if err != nil {
		fmt.Println("Error running command")
		fmt.Println(err)
owlo's avatar
owlo committed
175
		return false
owlo's avatar
owlo committed
176
	}
owlo's avatar
owlo committed
177
	return true
owlo's avatar
owlo committed
178
}
owlo's avatar
owlo committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

func runHackTrace(user string, target string, initializedActors []libs.ActorS) string {

	userList := strings.Split(user, ":")
	user = userList[0]
	//index := 0
	for i := range initializedActors {
		if initializedActors[i].PreferredUsername == user {
			//	index = i
			break
		}
	}
	//if initializedActors[i].Stat > hitCalc()
	//	return "OK"
	//else
	//  return "NO"
	return "OK"
}
owlo's avatar
owlo committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
func runBattleTrace(user string, target string, initializedActors []libs.ActorS) string {

	userList := strings.Split(user, ":")
	user = userList[0]
	//index := 0
	for i := range initializedActors {
		if initializedActors[i].PreferredUsername == user {
			//	index = i
			break
		}
	}
	//if initializedActors[i].Stat > hitCalc()
	//	return "OK"
	//else
	//  return "NO"
	return "OK"
}
214 215 216 217 218 219 220 221 222 223 224
func upBork(borks []libs.ActorS) []byte {
	borkFile, err := os.Create("tests/borks.json")
	defer borkFile.Close()
	if err != nil {
		fmt.Println("\033[38;2;255;0;0mBork error\033[0m")
	}
	borkJson, err := json.Marshal(borks)
	if err != nil {
		fmt.Println("error marshalling borks")
		fmt.Println(err)
	}
owlo's avatar
owlo committed
225
	//fmt.Println(borkJson)
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	borkFile.Write(borkJson)
	return borkJson

}
func updateBork(currentActors string) libs.ActorS {
	actorFile, err := os.Open(currentActors)
	defer actorFile.Close()
	if err != nil {
		fmt.Println("Error, cannot find current actor file.")
	}
	actorReader, err := ioutil.ReadAll(actorFile)
	if err != nil {
		fmt.Println("Error reading actorfile.")
	}
	var mockup libs.ActorS
	container := make(map[string]interface{})
	err = json.Unmarshal(actorReader, &mockup)
	err = json.Unmarshal(actorReader, &container)
	if err != nil {
		fmt.Println("Error unmarshalling json.")
		fmt.Println(err)
	}
	k := make([]string, len(container))
	index := 0
	for i, _ := range container {
		k[index] = i

		if k[index] == "@context" {
			con := container["@context"]
			mockup.Context = con
		}
		index++
	}
	//fmt.Println(k)
	//fmt.Println(string(actorReader))
owlo's avatar
owlo committed
261
	//fmt.Println(mockup)
262 263 264

	return mockup
}
Localtoast's avatar
Localtoast committed
265

266
func initActors(currentActors string) libs.ActorS {
267 268 269 270 271
	actorFile, err := os.Open(currentActors)
	defer actorFile.Close()
	if err != nil {
		fmt.Println("Error, cannot find current actor file.")
	}
owlo's avatar
owlo committed
272 273 274 275 276
	actorReader, err := ioutil.ReadAll(actorFile)
	if err != nil {
		fmt.Println("Error reading actorfile.")
	}
	var mockup libs.ActorS
277
	container := make(map[string]interface{})
owlo's avatar
owlo committed
278
	err = json.Unmarshal(actorReader, &mockup)
279
	err = json.Unmarshal(actorReader, &container)
owlo's avatar
owlo committed
280 281 282 283
	if err != nil {
		fmt.Println("Error unmarshalling json.")
		fmt.Println(err)
	}
284 285 286 287 288 289 290 291 292 293 294 295
	k := make([]string, len(container))
	index := 0
	for i, _ := range container {
		k[index] = i

		if k[index] == "@context" {
			con := container["@context"]
			mockup.Context = con
		}
		index++
	}
	//fmt.Println(k)
owlo's avatar
owlo committed
296
	//fmt.Println(string(actorReader))
owlo's avatar
owlo committed
297
	//fmt.Println(mockup)
298
	emptyString := mockup
299 300
	return emptyString
}
owlo's avatar
owlo committed
301
func UIDMaker(typeS string) string {
owlo's avatar
owlo committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	hostname := "localhost"
	username := "username"
	//Inspired by 'Una (unascribed)'s bikeshed
	rand.Seed(int64(time.Now().Nanosecond()))
	adjectives := []string{"Accidental", "Allocated", "Asymptotic", "Background", "Binary",
		"Bit", "Blast", "Blocked", "Bronze", "Captured", "Classic",
		"Compact", "Compressed", "Concatenated", "Conventional",
		"Cryptographic", "Decimal", "Decompressed", "Deflated",
		"Defragmented", "Dirty", "Distinguished", "Dozenal", "Elegant",
		"Encrypted", "Ender", "Enhanced", "Escaped", "Euclidean",
		"Expanded", "Expansive", "Explosive", "Extended", "Extreme",
		"Floppy", "Foreground", "Fragmented", "Garbage", "Giga", "Gold",
		"Hard", "Helical", "Hexadecimal", "Higher", "Infinite", "Inflated",
		"Intentional", "Interlaced", "Kilo", "Legacy", "Lower", "Magical",
		"Mapped", "Mega", "Nonlinear", "Noodle", "Null", "Obvious", "Paged",
		"Parity", "Platinum", "Primary", "Progressive", "Prompt",
		"Protected", "Quick", "Real", "Recursive", "Replica", "Resident",
		"Retried", "Root", "Secure", "Silver", "SolidState", "Super",
		"Swap", "Switched", "Synergistic", "Tera", "Terminated", "Ternary",
		"Traditional", "Unlimited", "Unreal", "Upper", "Userspace",
		"Vector", "Virtual", "Web", "WoodGrain", "Written", "Zipped"}
	nouns := []string{"AGP", "Algorithm", "Apparatus", "Array", "Bot", "Bus", "Capacitor",
		"Card", "Chip", "Collection", "Command", "Connection", "Cookie",
		"DLC", "DMA", "Daemon", "Data", "Database", "Density", "Desktop",
		"Device", "Directory", "Disk", "Dongle", "Executable", "Expansion",
		"Folder", "Glue", "Gremlin", "IRQ", "ISA", "Instruction",
		"Interface", "Job", "Key", "List", "MBR", "Map", "Modem", "Monster",
		"Numeral", "PCI", "Paradigm", "Plant", "Port", "Process",
		"Protocol", "Registry", "Repository", "Rights", "Scanline", "Set",
		"Slot", "Smoke", "Sweeper", "TSR", "Table", "Task", "Thread",
		"Tracker", "USB", "Vector", "Window"}
	uniquefier := ""

	uniqe := ""
	for i := 0; i < 2; i++ {
		uniq := rand.Intn(15)
		if uniq >= 10 {
			switch uniq {
			case 10:
				uniqe = "A"
			case 11:
				uniqe = "B"
			case 12:
				uniqe = "C"
			case 13:
				uniqe = "D"
			case 14:
				uniqe = "E"
			case 15:
				uniqe = "F"
			}

			uniquefier += uniqe
		} else {
			uniquefier += fmt.Sprint(uniq)
		}
	}
	ind := rand.Intn(len(adjectives))
	indie := rand.Intn(len(adjectives))
	if indie == ind {
		indie = rand.Intn(len(adjectives))
	}
	thedog := rand.Intn(len(nouns))
365
	uniqueFied := fmt.Sprint(uniquefier, hostname, username, adjectives[ind], adjectives[indie], nouns[thedog])
owlo's avatar
owlo committed
366

owlo's avatar
owlo committed
367
	//fmt.Println(uniqueFied)
owlo's avatar
owlo committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

	if typeS == "post" {
		numPosts, err := ioutil.ReadDir("tests/posts/")
		if err != nil {
			fmt.Println("Error, can't find post directory!")
		}
		UID := fmt.Sprint("posts/", len(numPosts)+1, uniqueFied)
		return UID
	}
	if typeS == "user" {
		numUsers, err := ioutil.ReadDir("tests/users/")
		if err != nil {
			fmt.Println("Error, can't find post directory!")
		}
		UID := fmt.Sprint("users/", len(numUsers)+1, uniqueFied)
		return UID
384
	}
owlo's avatar
owlo committed
385
	return "NOT NUMBERWANG"
owlo's avatar
owlo committed
386
}
387
func main() {
Localtoast's avatar
Localtoast committed
388

389 390 391 392 393
	fmt.Println("\033[48;2;0;255;0m\033[38;2;255;0;0mServer started\033[0m")
	dirBorks, err := ioutil.ReadDir("tests/posts/")
	if err != nil {
		fmt.Println("No borks!")
	}
owlo's avatar
owlo committed
394
	fmt.Println("DIRBORKS:", dirBorks)
owlo's avatar
owlo committed
395 396 397 398 399 400 401
	//room init
	xvar := 17
	yvar := 17
	room := flour.Dough(xvar, yvar)
	room = flour.Oven(room, "X", xvar, yvar)
	room[122].Label = "player"
	//end room init
owlo's avatar
owlo committed
402 403
	//numBorks := len(dirBorks)
	//numStringBorks := strconv.Itoa(numBorks)
404
	fmt.Println("\033[38;2;20;20;255mInitializing actor accounts\033[0m")
405 406 407 408 409 410 411 412 413
	currentActors, err := ioutil.ReadDir("tests/users/")
	if err != nil {
		fmt.Println("Error reading the user directory!")
	}
	var initializedActors []libs.ActorS
	for i := range currentActors {

		initializedActors = append(initializedActors, initActors("tests/users/"+currentActors[i].Name()))
	}
owlo's avatar
owlo committed
414
	//fmt.Println(initializedActors)
415
	fmt.Println("\033[48;2;255;20;20mInitialization Complete\033[0m")
owlo's avatar
owlo committed
416
	//fmt.Println(numStringBorks)
417
	//	fmt.Println("\033[38;2;0;255;0mThe most recent post is " + numBorks + "\033[0m")
Localtoast's avatar
Localtoast committed
418
	for {
Localtoast's avatar
Localtoast committed
419
		mux := http.NewServeMux()
420
		mux.HandleFunc("/exampleobject", func(w http.ResponseWriter, req *http.Request) {
Localtoast's avatar
Localtoast committed
421 422 423 424
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
Localtoast's avatar
Localtoast committed
425 426
			if req.Header.Get("Accept") == "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" {
				fmt.Println("Accept header is correct.")
427
				w.Header().Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
Localtoast's avatar
Localtoast committed
428
				//w.Header().Set("Authentication", signature)
429
				w.WriteHeader(http.StatusOK)
owlo's avatar
owlo committed
430
				fmt.Println("Signing test json.")
owlo's avatar
owlo committed
431 432 433 434
				signedContent, err := ioutil.ReadFile("tests/post.json")
				if err != nil {
					fmt.Println("Error reading post.")
				}
owlo's avatar
owlo committed
435
				//fmt.Println(string(signedContent))
owlo's avatar
owlo committed
436
				fmt.Println("Signing complete.")
Localtoast's avatar
Localtoast committed
437
				w.Write(signedContent)
438
			}
Localtoast's avatar
Localtoast committed
439
		})
440 441 442 443 444 445 446 447 448

		mux.HandleFunc("/getuid", func(w http.ResponseWriter, req *http.Request) {
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
			if req.Header.Get("Accept") == "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" {
				fmt.Println("Accept header is correct.")
				w.Header().Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
owlo's avatar
owlo committed
449
				UID := UIDMaker("post")
450 451 452
				w.Header().Set("UID", UID)
				w.WriteHeader(http.StatusOK)
				fmt.Println("Signing test json.")
owlo's avatar
owlo committed
453 454 455 456
				signedContent, err := ioutil.ReadFile("tests/post.json")
				if err != nil {
					fmt.Println("Error reading file")
				}
owlo's avatar
owlo committed
457
				//fmt.Println(string(signedContent))
458 459 460 461 462
				fmt.Println("Signing complete.")
				w.Write(signedContent)
			}
		})

owlo's avatar
owlo committed
463
		mux.HandleFunc("/exampleMobile", func(w http.ResponseWriter, req *http.Request) {
464 465
			//AVATARCOMMANDS

owlo's avatar
owlo committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
			if req.Header.Get("Accept") == "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" {
				fmt.Println("Accept header is correct.")
				w.Header().Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
				//w.Header().Set("Authentication", signature)
				mobile := req.Header.Get("Mobile-Name")
				w.WriteHeader(http.StatusOK)
				fmt.Println("Signing test json.")
				if err != nil {
					fmt.Println("Error, user directory empty!")
				}
				createMobile(mobile)
				if err != nil {
					fmt.Println("Error reading file")
				}
				w.Write([]byte("bep"))
			}
		})

owlo's avatar
owlo committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
		mux.HandleFunc("/examplePC", func(w http.ResponseWriter, req *http.Request) {
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
			if req.Header.Get("Accept") == "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" {
				fmt.Println("Accept header is correct.")
				w.Header().Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
				//w.Header().Set("Authentication", signature)
				w.WriteHeader(http.StatusOK)
				fmt.Println("Signing test json.")
				names, err := ioutil.ReadDir("tests/users/")
				if err != nil {
					fmt.Println("Error, user directory empty!")
				}
owlo's avatar
owlo committed
503 504 505 506
				signedContent, err := ioutil.ReadFile("tests/users/" + names[0].Name())
				if err != nil {
					fmt.Println("Error reading file")
				}
owlo's avatar
owlo committed
507
				//fmt.Println(string(signedContent))
owlo's avatar
owlo committed
508 509 510 511 512
				fmt.Println("Signing complete.")
				w.Write(signedContent)
			}
		})

513 514 515 516 517 518 519 520 521 522 523
		mux.HandleFunc("/exampleactor", func(w http.ResponseWriter, req *http.Request) {
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
			if req.Header.Get("Accept") == "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" {
				fmt.Println("Accept header is correct.")
				w.Header().Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
				//w.Header().Set("Authentication", signature)
				w.WriteHeader(http.StatusOK)
				fmt.Println("Signing test json.")
owlo's avatar
owlo committed
524 525 526 527
				signedContent, err := ioutil.ReadFile("tests/post.json")
				if err != nil {
					fmt.Println("Error reading file")
				}
owlo's avatar
owlo committed
528
				//fmt.Println(string(signedContent))
529 530 531 532
				fmt.Println("Signing complete.")
				w.Write(signedContent)
			}
		})
owlo's avatar
owlo committed
533 534 535 536 537 538 539 540 541
		mux.HandleFunc("/getBattleTrace", func(w http.ResponseWriter, req *http.Request) {
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
			fmt.Println("WHO FIGHTING?")
			signed := req.Header.Get("Signature")
			target := req.Header.Get("Target")
			//			signature := req.Header.Get("Authorization")
owlo's avatar
owlo committed
542
			//signature, err := ioutil.ReadAll(req.Body)
owlo's avatar
owlo committed
543
			//fmt.Println(string(signed))
owlo's avatar
owlo committed
544 545 546
			if err != nil {
				fmt.Println(err)
			}
owlo's avatar
owlo committed
547 548
			//ok := validateLogin(signed, []byte(signature), initializedActors)
			ok := true
owlo's avatar
owlo committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562
			if ok {
				w.Header().Set("Authorization", "ok")
				w.WriteHeader(http.StatusOK)
				//fmt.Println("GOT" + signed)

				results := runBattleTrace(signed, target, initializedActors)
				w.Header().Set("HackTrace", results)
				w.Header().Set("Target", results)
				w.Write([]byte(results))
			} else {
				w.Header().Set("Authorization", "not authorized")
				w.Write([]byte("ennt!"))
			}
		})
owlo's avatar
owlo committed
563 564 565 566 567 568 569 570 571
		mux.HandleFunc("/getHackTrace", func(w http.ResponseWriter, req *http.Request) {
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
			fmt.Println("WHAT DOING?")
			signed := req.Header.Get("Signature")
			target := req.Header.Get("Target")
			//			signature := req.Header.Get("Authorization")
owlo's avatar
owlo committed
572
			//	signature, err := ioutil.ReadAll(req.Body)
owlo's avatar
owlo committed
573
			//fmt.Println(string(signed))
owlo's avatar
owlo committed
574 575 576
			if err != nil {
				fmt.Println(err)
			}
owlo's avatar
owlo committed
577 578
			//ok := validateLogin(signed, []byte(signature), initializedActors)
			ok := true
owlo's avatar
owlo committed
579 580 581 582 583 584 585 586 587 588 589
			if ok {
				w.Header().Set("Authorization", "ok")
				w.WriteHeader(http.StatusOK)
				fmt.Println("GOT" + signed)
				results := runHackTrace(signed, target, initializedActors)
				w.Write([]byte(results))
			} else {
				w.Header().Set("Authorization", "not authorized")
				w.Write([]byte("ennt!"))
			}
		})
owlo's avatar
owlo committed
590 591 592 593 594 595
		mux.HandleFunc("/getMap", func(w http.ResponseWriter, req *http.Request) {
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
			fmt.Println("WHER GOING?")
owlo's avatar
owlo committed
596
			//signed := req.Header.Get("Signature")
owlo's avatar
owlo committed
597
			//			signature := req.Header.Get("Authorization")
owlo's avatar
owlo committed
598
			//	signature, err := ioutil.ReadAll(req.Body)
owlo's avatar
owlo committed
599
			//fmt.Println(string(signed))
owlo's avatar
owlo committed
600 601 602 603 604
			//	if err != nil {
			//		fmt.Println(err)
			//	}
			//ok := validateLogin(signed, []byte(signature), initializedActors)
			ok := true
owlo's avatar
owlo committed
605 606
			if ok {
				w.Header().Set("Authorization", "ok")
owlo's avatar
owlo committed
607
				dir := req.Header.Get("Direction")
owlo's avatar
owlo committed
608 609 610

				w.Header().Set("Room Desc", "A lone room in a love hotel:There is little in this room of interest.:A door into the verse stands to the east.")
				w.Header().Set("Room Actions", "Your body is rezzed into being.")
owlo's avatar
owlo committed
611
				w.WriteHeader(http.StatusOK)
owlo's avatar
owlo committed
612

owlo's avatar
owlo committed
613 614 615 616
				switch dir {
				case "east":
					for i := range room {
						if room[i].Label == "player" {
owlo's avatar
owlo committed
617 618 619 620 621 622
							if room[i+1].Label != "X" {
								room[i+1].Label = "player"
								room[i].Label = ""
								break
							}
							fmt.Printf("You cannot go that way!")
owlo's avatar
owlo committed
623 624 625 626 627
						}
					}
				case "west":
					for i := range room {
						if room[i].Label == "player" {
owlo's avatar
owlo committed
628 629 630 631 632 633 634
							if room[i-1].Label != "X" {
								room[i-1].Label = "player"
								room[i].Label = ""
								break
							}
							fmt.Printf("You cannot go that way!")

owlo's avatar
owlo committed
635 636 637 638 639
						}
					}
				case "north":
					for i := range room {
						if room[i].Label == "player" {
owlo's avatar
owlo committed
640 641 642 643 644 645 646
							if room[i-xvar].Label != "X" {
								room[i-xvar].Label = "player"
								room[i].Label = ""
								break
							}
							fmt.Printf("You cannot go that way!")

owlo's avatar
owlo committed
647 648 649 650 651
						}
					}
				case "south":
					for i := range room {
						if room[i].Label == "player" {
owlo's avatar
owlo committed
652 653 654 655 656 657 658
							if room[i+xvar].Label != "X" {
								room[i+xvar].Label = "player"
								room[i].Label = ""
								break
							}
							fmt.Printf("You cannot go that way!")

owlo's avatar
owlo committed
659 660 661 662 663
						}
					}
				case "northeast":
					for i := range room {
						if room[i].Label == "player" {
owlo's avatar
owlo committed
664 665 666 667 668 669 670
							if room[i-xvar+1].Label != "X" {
								room[i-xvar+1].Label = "player"
								room[i].Label = ""
								break
							}
							fmt.Printf("You cannot go that way!")

owlo's avatar
owlo committed
671 672 673 674 675
						}
					}
				case "northwest":
					for i := range room {
						if room[i].Label == "player" {
owlo's avatar
owlo committed
676 677 678 679 680 681 682
							if room[i-xvar-1].Label != "X" {
								room[i-xvar-1].Label = "player"
								room[i].Label = ""
								break
							}
							fmt.Printf("You cannot go that way!")

owlo's avatar
owlo committed
683 684 685 686 687
						}
					}
				case "southeast":
					for i := range room {
						if room[i].Label == "player" {
owlo's avatar
owlo committed
688 689 690 691 692 693 694
							if room[i+xvar+1].Label != "X" {
								room[i+xvar+1].Label = "player"
								room[i].Label = ""
								break
							}
							fmt.Printf("You cannot go that way!")

owlo's avatar
owlo committed
695 696 697 698 699
						}
					}
				case "southwest":
					for i := range room {
						if room[i].Label == "player" {
owlo's avatar
owlo committed
700 701 702 703 704 705 706
							if room[i+xvar-1].Label != "X" {
								room[i+xvar-1].Label = "player"
								room[i].Label = ""
								break
							}
							fmt.Printf("You cannot go that way!")

owlo's avatar
owlo committed
707 708 709
						}
					}
				}
owlo's avatar
owlo committed
710
				space := ""
owlo's avatar
owlo committed
711 712
				for x := range room {
					done := false
owlo's avatar
owlo committed
713
					if room[x].Label == "player" {
owlo's avatar
owlo committed
714
						space += "@"
owlo's avatar
owlo committed
715 716 717 718
						done = true
					}
					if room[x].X > 3 && room[x].Y == 7 && room[x].X < 14 && done == false {
						space += " "
owlo's avatar
owlo committed
719
						room[x].Label = " "
owlo's avatar
owlo committed
720 721 722 723
						done = true
					}
					if room[x].X > 3 && room[x].Y == 8 && room[x].X < 14 && done == false {
						space += " "
owlo's avatar
owlo committed
724
						room[x].Label = " "
owlo's avatar
owlo committed
725 726 727 728 729
						done = true
					}
					if room[x].X > 3 && room[x].Y == 6 && room[x].X < 14 && done == false {
						space += " "
						done = true
owlo's avatar
owlo committed
730
						room[x].Label = " "
owlo's avatar
owlo committed
731 732 733
					}
					if done == false {
						space += "X"
owlo's avatar
owlo committed
734
						room[x].Label = "X"
owlo's avatar
owlo committed
735 736 737 738 739 740 741 742 743
					}
				}
				w.Write([]byte(space))
			} else {
				w.Header().Set("Authorization", "not authorized")
				w.Write([]byte("ennt!"))
			}
		})

744 745 746 747 748 749
		mux.HandleFunc("/examplelogin", func(w http.ResponseWriter, req *http.Request) {
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
			fmt.Println("I HEAR BORK")
owlo's avatar
owlo committed
750
			//signed := req.Header.Get("Signature")
owlo's avatar
owlo committed
751 752
			//			//signature := req.Header.Get("Authorization")
			//	signature, err := ioutil.ReadAll(req.Body)
owlo's avatar
owlo committed
753
			//fmt.Println(string(signed))
owlo's avatar
owlo committed
754 755 756 757 758
			//	if err != nil {
			//		fmt.Println(err)
			//	}
			//	ok := validateLogin(signed, []byte(//signature), initializedActors)
			ok := true
owlo's avatar
owlo committed
759 760 761 762 763
			if ok {
				w.Header().Set("Authorization", "ok")
				w.WriteHeader(http.StatusOK)
				w.Write([]byte("bop"))
			} else {
owlo's avatar
owlo committed
764 765
				w.Header().Set("Authorization", "not authorized")
				w.Write([]byte("ennt!"))
owlo's avatar
owlo committed
766
			}
767
		})
owlo's avatar
owlo committed
768 769 770 771 772
		mux.HandleFunc("/examplepost", func(w http.ResponseWriter, req *http.Request) {
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
773 774 775 776 777 778 779 780
			posts, err := ioutil.ReadDir("tests/posts/")
			if err != nil {
				fmt.Println(err)
			}
			numPosts := strconv.Itoa(len(posts))
			w.Header().Set("NumBorks", numPosts)
			w.WriteHeader(http.StatusOK)
			fmt.Println("Signing test json.")
owlo's avatar
owlo committed
781 782 783
			if req.Header.Get("Accept") == "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" {
				fmt.Println("Accept header is correct.")
				w.Header().Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
owlo's avatar
owlo committed
784
				//w.Header().Set("Authentication", //signature)
785 786 787 788

				var borks []libs.ActorS
				for _, v := range posts {
					borks = append(borks, updateBork("tests/posts/"+v.Name()))
789

790
				}
791
				upBork(borks)
owlo's avatar
owlo committed
792 793 794 795
				signedContent, err := ioutil.ReadFile("tests/borks.json")
				if err != nil {
					fmt.Println("Error reading file")
				}
owlo's avatar
owlo committed
796
				//fmt.Println(string(signedContent))
797 798 799
				fmt.Println("Signing complete.")
				w.Write(signedContent)

owlo's avatar
owlo committed
800
			}
owlo's avatar
owlo committed
801

owlo's avatar
owlo committed
802 803 804 805 806 807
		})
		mux.HandleFunc("/post", func(w http.ResponseWriter, req *http.Request) {
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
owlo's avatar
owlo committed
808
			//w.Header().Set("NumBorks", numPosts)
owlo's avatar
owlo committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822
			w.WriteHeader(http.StatusOK)
			fmt.Println("Signing test json.")
			if req.Header.Get("Accept") == "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" {
				fmt.Println("Accept header is correct.")
				w.Header().Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
				//w.Header().Set("Authentication", //signature)

				signedContent, err := ioutil.ReadAll(req.Body)
				if err != nil {
					panic(err)
				} else {
					output := fmt.Sprint("======================", signedContent, "========================")
					fmt.Println(output)
				}
owlo's avatar
owlo committed
823 824
				//TODO
				name := UIDMaker("post")
owlo's avatar
owlo committed
825
				file, err := os.Create("tests/"+name)
owlo's avatar
owlo committed
826
				defer file.Close()
owlo's avatar
owlo committed
827
				numBytes, err := file.WriteString(string(signedContent))
owlo's avatar
owlo committed
828
				if err != nil {
owlo's avatar
owlo committed
829
					fmt.Println(name)
owlo's avatar
owlo committed
830
					fmt.Println("ERROR CREATING FILE")
owlo's avatar
owlo committed
831 832 833 834 835
					panic(err)
				}else {
					fmt.Println("Wrote ", numBytes, " bytes")
					file.Sync()
				}
owlo's avatar
owlo committed
836 837 838 839 840 841 842 843 844 845
				var borks []libs.ActorS
				posts, err := ioutil.ReadDir("tests/posts/")
				if err != nil {
					fmt.Println(err)
				}
				for _, v := range posts {
					borks = append(borks, updateBork("tests/posts/"+v.Name()))

				}
				//numPosts := strconv.Itoa(len(posts))
owlo's avatar
owlo committed
846 847 848 849 850
				upBork(borks)
				w.Write(signedContent)

			}

owlo's avatar
owlo committed
851
		})
owlo's avatar
owlo committed
852 853 854 855

		//CREATE USER

		//END CREATE USER
owlo's avatar
owlo committed
856 857 858 859 860 861 862
		//		mux.HandleFunc("/.well-known/webfinger?resource=acct:guarddoggo@example.com", func(w http.ResponseWriter, req *http.Request) {
		// Before any call to WriteHeader or Write, declare
		// the trailers you will set during the HTTP
		// response. These three headers are actually sent in
		// the trailer.
		//			fmt.Println("SOMEONE IS PREPARING TO BORK.")

owlo's avatar
owlo committed
863 864 865 866 867 868
		mux.HandleFunc("/exampleposter", func(w http.ResponseWriter, req *http.Request) {
			// Before any call to WriteHeader or Write, declare
			// the trailers you will set during the HTTP
			// response. These three headers are actually sent in
			// the trailer.
			fmt.Println("SOMEONE IS BORKING.")
owlo's avatar
owlo committed
869 870 871

			//defer resp.Body.Close()

owlo's avatar
owlo committed
872
			object, err := ioutil.ReadAll(req.Body)
owlo's avatar
owlo committed
873
			req.Body.Close()
owlo's avatar
owlo committed
874
			if err != nil {
owlo's avatar
owlo committed
875 876
				fmt.Println("Error reading reponse.")
				fmt.Println(err)
owlo's avatar
owlo committed
877
			}
owlo's avatar
owlo committed
878 879
			//beachBall := object
			//fmt.Println(beachBall)
owlo's avatar
owlo committed
880
			UID := UIDMaker("post")
owlo's avatar
owlo committed
881
			validate(UID, object)
owlo's avatar
owlo committed
882

owlo's avatar
owlo committed
883
		})
Localtoast's avatar
Localtoast committed
884
		prefix := os.Getenv("GOPATH")
owlo's avatar
owlo committed
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

		mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
			w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
			w.Write([]byte("This is an example server.\n"))
		})
		cfg := &tls.Config{
			MinVersion:               tls.VersionTLS12,
			CurvePreferences:         []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
			PreferServerCipherSuites: true,
			CipherSuites: []uint16{
				tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
				tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
				tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
				tls.TLS_RSA_WITH_AES_256_CBC_SHA,
			},
		}
		srv := &http.Server{
			Addr:         ":8080",
			Handler:      mux,
			TLSConfig:    cfg,
			TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
		}
Localtoast's avatar
Localtoast committed
907
		log.Fatal(srv.ListenAndServeTLS(prefix+"/gitlab.com/owlo/server.rsa.crt", prefix+"/gitlab.com/owlo/server.rsa.key"))
owlo's avatar
owlo committed
908

Localtoast's avatar
Localtoast committed
909
	}
910
}