func (mux *Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { guidRequest := &GUIDRequest{} d := json.NewDecoder(request.Body) if err := d.Decode(&guidRequest); err != nil { log.Error("Error decoding JSON: ", err) http.Error(writer, err.Error(), 500) } dynamo := dynamodb.Server{ Auth: awsAuth, Region: aws.USEast, } locationsTable := dynamo.NewTable(dynamoTable, pk) // lls, err := locationsTable.Scan([]dynamodb.AttributeComparison{ // *dynamodb.NewStringAttributeComparison("hash", dynamodb.COMPARISON_NOT_EQUAL, "asdf"), // }) // for _, ll := range lls { // log.Info(ll["hash"].Value) // } digest := sha256.Sum256([]byte(guidRequest.Guid)) hashKey := hex.EncodeToString(digest[:]) // item, err := locationsTable.GetItem(&dynamodb.Key{HashKey: hashKey}) item, err := locationsTable.GetItem(&dynamodb.Key{HashKey: guidRequest.Guid}) if err != nil { log.Error("Error getting item from table: ", err) } location := &LocationResponse{ Hash: hashKey, Bucket: item["bucket"].Value, RandLine: item["randLine"].Value, } if infoLog { log.Infof("S3 key: %s", location.Bucket) } response := GUIDResponse{ GuidHash: location.Hash, BucketName: location.Bucket, } jsonResponse, err := json.Marshal(response) if err != nil { log.Error("Error marshalling JSON: ", err) } // locations <- location go processLog(location) writer.Write(jsonResponse) }
func NewSCServer(addr string) SCServer { listener, err := net.Listen("tcp", addr) if err != nil { panic(err.Error()) } killer := make(chan bool) go func() { for { select { case <-killer: listener.Close() return default: // establish connection client, err := listener.Accept() //log.Debug("Of acceptings client: %s", client.RemoteAddr()) if err != nil { log.Error(err.Error()) client.Close() continue } go func() { err := sc_server_handler(client) if err != nil { //log.Error(err.Error()) } }() } } }() return SCServer{listener, killer} }
func sc_server_handler(wire net.Conn) error { defer wire.Close() owire, err := kiss.Kiriobfs_handshake_server(wire) if err != nil { //log.Error(err.Error()) return err } log.Debug("Of dones for obfs layer") awire, err := kiss.KiSS_handshake_server(owire, MasterKey) if err != nil { //log.Error(err.Error()) return err } log.Debug("Of dones in kiss layer") // Now awire is the wire cmd, err := read_sc_message(awire) if err != nil { log.Error(err.Error()) return err } log.Debug(cmd) if cmd.Msg_type == SC_EXTEND { theirnode := dirclient.PKeyLookup(cmd.Msg_arg) if theirnode == nil { return errors.New("Watif") } actwire, err := net.Dial("tcp", theirnode.Address) if err != nil { return err } remwire, err := kiss.Kiriobfs_handshake_client(actwire) if err != nil { return err } go func() { io.Copy(remwire, awire) remwire.Close() }() io.Copy(awire, remwire) awire.Close() } else if cmd.Msg_type == SC_TERMINATE && MasterConfig.General.IsExit { e2e_server_handler(newGobWire(awire)) } return nil }
func run_monitor_loop() { listener, err := net.Listen("tcp", "127.0.0.1:9221") if err != nil { panic(err.Error()) } for { client, err := listener.Accept() if err != nil { log.Error(err.Error()) continue } func() { defer client.Close() for { thing := <-global_monitor_chan _, err := client.Write(thing) if err != nil { return } } }() } }
func processLog(location *LocationResponse) { // log.Info("Line number: ", location.RandLine) lineNumber, err := strconv.Atoi(location.RandLine) if err != nil { log.Error("Error converting randLine to int: ", err) return } reader, err := s3.New(awsAuth, awsRegion).Bucket(s3Bucket).GetReader(location.Bucket) bufReader := bufio.NewReader(reader) defer reader.Close() s3KeyParts := strings.SplitN(location.Bucket, "_", 7) filenameTimestamp, err := time.Parse(filenameTimeLayout, s3KeyParts[5]) if err != nil { log.Error("Error parsing filename timestamp: ", err) return } if infoLog { log.Info("Filename timestamp: ", filenameTimestamp.String()) } var sumT int = 0 sha := sha256.New() for i := 0; i < lineNumber; i++ { line, _, err := bufReader.ReadLine() if err != nil { log.Error("Error reading from S3 buffered: ", err) return } splitLine := bytes.SplitN(line, logSeparator, 14) sha.Write(splitLine[12]) logTimestamp, err := time.Parse(logTimeLayout, string(splitLine[0])) if err != nil { log.Error("Error parsing log line timestamp: ", err) return } if infoLog { log.Info("Log timestamp: ", logTimestamp.String()) } timeDifference := int(filenameTimestamp.Sub(logTimestamp).Seconds()) if timeDifference < 0 { timeDifference = 0 } if infoLog { log.Infof("Time diff: %d", timeDifference) } sumT += timeDifference } if infoLog { log.Infof("Time difference sum: %d", sumT) } cksum := sha.Sum(nil) if infoLog { log.Info("cksum1: ", hex.EncodeToString(cksum)) } for i := 0; i < sumT+1; i++ { sha := sha256.New() sha.Write(cksum) cksum = sha.Sum(nil) } message, err := json.Marshal(SQSMessage{ GuidHash: location.Hash, ResultHash: hex.EncodeToString(cksum), }) if err != nil { log.Error("Error marshalling SQS message JSON: ", err) } if infoLog { log.Info("SQS message: ", string(message)) } queue, err := sqs.New(awsAuth, awsRegion).GetQueue(queueName) if err != nil { log.Error("Error getting queue: ", err) } queue.SendMessage(string(message)) }