示例#1
0
func (this *Validator) Start() {
	ruleFilePath := path.Join(this.rulePath, "validate.json")
	dbFilePath := path.Join(this.dbPath, "agent.db")
	updateTableName := `"update"`
	validateTableName := `"validate"`

	db, err := util.InitTable(fmt.Sprintf(
		"PRAGMA journal_mode = WAL; CREATE TABLE IF NOT EXISTS %s(ip TEXT, port TEXT, domain TEXT, level INTEGER)",
		validateTableName,
	), dbFilePath)

	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	level := util.GetLastLevel(validateTableName, db) + 1

	qe := engine.NewQuickEngine(ruleFilePath)
	reqs := this.genRequests(qe.GetQuickEngineConfig().StartUrls, updateTableName, level, db)

	qe.
		GetEngine().
		SetStartRequests(reqs).
		AddPlugin(util.NewModifyResponsePlugin()).
		AddPlugin(util.NewAddLevelPlugin(level)).
		SetPipeline(pipeline.NewSqlPipeline(db, validateTableName)).
		Start()
}
示例#2
0
func (this *Updater) Start() {
	ruleFilePath := path.Join(this.rulePath, "update.json")
	dbFilePath := path.Join(this.dbPath, "agent.db")
	tableName := `"update"`

	db, err := util.InitTable(fmt.Sprintf(
		"PRAGMA journal_mode = WAL; CREATE TABLE IF NOT EXISTS %s(ip TEXT, port TEXT, source TEXT, level INTEGER)",
		tableName,
	), dbFilePath)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	level := util.GetLastLevel(tableName, db) + 1

	e := engine.
		NewQuickEngine(ruleFilePath).
		GetEngine().
		AddPlugin(util.NewAddLevelPlugin(level)).
		SetPipeline(pipeline.NewSqlPipeline(db, tableName))

	var ok bool
	if ok = this.isAgentServerOK(); ok {
		e.AddPlugin(plugin.NewProxyPlugin())
	}
	log.Printf("started %s(isAgentServerOK: %v)\n", ruleFilePath, ok)
	e.Start()
}
示例#3
0
func NewServer(dbPath string, port int) *Server {
	dbFilePath := path.Join(dbPath, "agent.db")
	validateTableName := `"validate"`
	db, err := util.InitTable(fmt.Sprintf(
		"PRAGMA journal_mode = WAL; CREATE TABLE IF NOT EXISTS %s(ip TEXT, port TEXT, domain TEXT, level INTEGER)",
		validateTableName,
	), dbFilePath)
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		sigChan := make(chan os.Signal, 1)
		signal.Notify(sigChan, os.Interrupt)
		<-sigChan
		log.Println("Closing database...")
		db.Close()
		os.Exit(0)
	}()

	return &Server{
		port:      port,
		tableName: validateTableName,
		db:        db,
	}
}