Ejemplo n.º 1
0
func initDb() (err error) {
	// connect to db using standard Go database/sql API
	db, err := sql.Open("sqlite3", DB_FILE)
	if err != nil {
		return
	}
	defer db.Close()

	// construct a gorp DbMap
	dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}

	addTable(dbmap)

	// create the table. in a production system you'd generally
	// use a migration tool, or create the tables via scripts
	err = dbmap.CreateTables()
	if err != nil {
		return
	}

	//init data
	var option SysOption
	option.Name = "mode"
	option.Value = "forward"
	err = dbmap.Insert(&option)
	if err != nil {
		return
	}
	var servers []string
	servers, err = util.GetDnsServer()
	if err != nil {
		return
	}
	log.Println("dnsserver:", servers)
	for _, server := range servers {
		fs := ForwardServer{Ip: server}
		err = dbmap.Insert(&fs)
		if err != nil {
			return
		}
	}
	var user User
	user.Name = "admin"
	user.Pwd = "admin"
	err = dbmap.Insert(&user)
	if err != nil {
		return
	}

	return
}
Ejemplo n.º 2
0
func main() {
	//initialize log
	log.SetOutput(os.Stdout)
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	//initialize resolver addrs
	dnsServers, err := util.GetDnsServer()
	if err != nil {
		log.Println(err)
		return
	}
	var resolverAddrs []string
	for _, dnsServer := range dnsServers {
		resolverAddrs = append(resolverAddrs, dnsServer+":53")
	}

	//initialize session
	gsession.buffer = make(map[SessionKey]SessionValue)

	//initialize db
	err = db.InitDb()
	if err != nil {
		log.Println(err)
		return
	}

	//listen for service
	addr := net.UDPAddr{Port: 53}
	server, err := net.ListenUDP("udp", &addr)
	if err != nil {
		log.Println(err)
		return
	}
	defer server.Close()

	//initialize forward conn
	forwardConns := make([]*net.UDPConn, FORWARD_PORT_NUM)
	for i := 0; i < FORWARD_PORT_NUM; i++ {
		forwardConn, err := net.ListenUDP("udp", &net.UDPAddr{})
		if err != nil {
			log.Println(err)
			return
		}
		forwardConns[i] = forwardConn
		go doRecvFromResolver(server, forwardConn)
		defer forwardConn.Close()
	}

	recvMsgChannel := make(chan RecvMsg, 10)
	go doForwardToResolver(server, forwardConns, resolverAddrs, recvMsgChannel)

	//start web
	go web.Start()

	buf := make([]byte, 65535)
	for {
		length, client, err := server.ReadFromUDP(buf)
		if err != nil {
			log.Println(err)
			continue
		}
		//log.Println(buf[:length])
		recvMsgChannel <- RecvMsg{addr: *client, data: buf[:length]}
	}
}