Ejemplo n.º 1
0
func Setup() {
	var err error
	cfg := config.Read()

	connectionString := cfg.DB.ConnectionString
	if cfg.DB.Type == "sqlite3" {
		connectionString = config.ConfigFile(connectionString)
		log.Println("Using SQLite DB at", connectionString)
	}
	for {
		db, err = gorm.Open(cfg.DB.Type, connectionString)

		if err == nil {
			break
		} else {
			log.Println("Cannot connect to DB. Will try again in 2 seconds:", err)
			time.Sleep(time.Duration(2) * time.Second)
		}
	}

	db.AutoMigrate(
		&User{},
		&CredRecord{},
		&Peer{},
		&SentMessage{},
	)

	db.Model(&CredRecord{}).AddUniqueIndex("idx_cred_hash", "cred_hash")
	db.Model(&CredRecord{}).AddIndex("idx_statement_hash", "statement_hash")
	db.Model(&SentMessage{}).AddUniqueIndex("idx_message_hash", "message_hash")
	db.Model(&User{}).AddUniqueIndex("idx_fingerprint", "fingerprint")
}
Ejemplo n.º 2
0
func StartWeb(wg sync.WaitGroup) {
	defer wg.Done()

	cfg := config.Read()
	listenUri := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)

	mux := http.NewServeMux()

	mux.HandleFunc("/creds/info", web.RawCredHandler)
	mux.HandleFunc("/creds/info/", web.InfoCredHandler)
	mux.HandleFunc("/creds", CredHandler)

	mux.HandleFunc("/users/", UsersHandler)

	mux.HandleFunc("/connect", ConnectHandler)
	mux.HandleFunc("/ping", PingHandler)
	mux.HandleFunc("/protocol-handler", ProtocolHandlerHandler)

	static := http.FileServer(http.Dir(config.AssetDir("htdocs")))
	mux.Handle("/", static)

	server := &http.Server{
		Addr:    listenUri,
		Handler: mux,
	}

	log.Println("Webservice will start on", listenUri)
	if cfg.Application.OpenWebUIOnStart {
		log.Println("Opening web browser…")
		// TODO: Create proper welcome page
		webbrowser.Open(fmt.Sprintf("%s", listenUri))
	}
	panic(server.ListenAndServe())
}
Ejemplo n.º 3
0
func Setup() {
	config := config.Read()
	broadcastUri := fmt.Sprintf("tcp://%s:%d", config.Broadcaster.Host, config.Broadcaster.Port)

	var err error
	broadcaster, err = goczmq.NewPub(broadcastUri)
	if err != nil {
		panic(err)
	}
	log.Println("Broadcaster will start on", broadcastUri)

	pitcher = goczmq.NewSock(goczmq.Push)

	receiver, err = goczmq.NewPull("inproc://broadcast")
	if err != nil {
		panic(err)
	}

	helpers.ConnectToPeers(true, ConnectToBroadcatcher)
}
Ejemplo n.º 4
0
// Return: whether or not to rebroadcast the message
func ProcessInboundSearchRequest(searchRequest *credence.SearchRequest) bool {
	config := config.Read()
	// TODO: Store search request

	if searchRequest.Proximity <= config.SearchRequests.ForwardProximityLimit {
		searchRequest.Proximity += 1

		for _, key := range searchRequest.Keys {
			for _, cred := range models.SearchCreds(key) {
				credMsg := &credence.Message{
					Type: &credence.Message_Cred{
						Cred: cred,
					},
				}

				BroadcastMessage(credMsg)
			}
		}

		return true
	}
	return false
}
Ejemplo n.º 5
0
func SearchCredsBreakdown(key string) []*credence.SearchResult_SourceBreakdown {
	var results []*credence.SearchResult_SourceBreakdown

	cfg := config.Read()

	var dbSpecificSelect string
	switch cfg.DB.Type {
	case "postgres":
		dbSpecificSelect = "source_uri, statement_hash, cred_bytes, sum(no_comment::int * coalesce(a.weight, 1)), sum(is_true::int * coalesce(a.weight, 1)), sum(is_false::int * coalesce(a.weight, 1)), sum(is_ambiguous::int * coalesce(a.weight, 1)), sum(coalesce(a.weight, 0))"
	case "sqlite3":
		dbSpecificSelect = "source_uri, statement_hash, cred_bytes, sum(no_comment * coalesce(a.weight, 1)), sum(is_true * coalesce(a.weight, 1)), sum(is_false * coalesce(a.weight, 1)), sum(is_ambiguous * coalesce(a.weight, 1)), sum(coalesce(a.weight, 0))"
	}

	rows, err := db.
		Table("cred_records").
		Select(dbSpecificSelect).
		Joins("left join users a on author_id = a.id").
		Where("source_uri LIKE ?", key).
		Group("source_uri, statement_hash, cred_bytes").
		Rows()

	if err != nil {
		panic(err)
	}

	for rows.Next() {
		var (
			sourceUri string
			breakdown credence.SearchResult_AssertionBreakdown
			credBytes []byte
		)

		rows.Scan(
			&sourceUri,
			&breakdown.StatementHash,
			&credBytes,
			&breakdown.NoComment,
			&breakdown.IsTrue,
			&breakdown.IsFalse,
			&breakdown.IsAmbiguous,
			&breakdown.Recognised,
		)

		cred := &credence.Cred{}
		err := proto.Unmarshal(credBytes, cred)
		if err != nil {
			panic(err)
		}
		// So much hack! The statement fields are in the same place, so we can convert by doing
		// lots of marshalling and unmarshalling…
		justCredStatement := &credence.Cred{
			Statement: cred.Statement,
		}
		statementBytes, err := proto.Marshal(justCredStatement)
		justBreakdownStatement := &credence.SearchResult_AssertionBreakdown{}
		err = proto.Unmarshal(statementBytes, justBreakdownStatement)
		breakdown.Statement = justBreakdownStatement.Statement
		// </hack>

		assertions := []*credence.SearchResult_AssertionBreakdown{&breakdown}
		sourceBreakdown := credence.SearchResult_SourceBreakdown{
			SourceUri:  sourceUri,
			Assertions: assertions,
		}
		results = append(results, &sourceBreakdown)
	}

	return results
}