-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxyinabox.go
66 lines (57 loc) · 1.23 KB
/
proxyinabox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package proxyinabox
import (
"github.com/jinzhu/gorm"
// mysql driver for GORM
_ "github.com/go-sql-driver/mysql"
)
//DB instance
var DB *gorm.DB
//CI cache instance
var CI Cache
//Conf config struct
type Conf struct {
Debug bool
MySQL struct {
Host string
Port string
User string
Pass string
Dbname string
} `mapstructure:"mysql"`
Redis struct {
Host string
Port string
Pass string
Db int
}
Sys struct {
Name string
ProxyVerifyWorker int `mapstructure:"proxy_verify_worker"`
DomainsPerIP int `mapstructure:"domains_per_ip"`
RequestLimitPerIP int64 `mapstructure:"request_limit_per_ip"`
VerifyDuration int `mapstructure:"verify_duration"`
}
}
//Config system config
var Config Conf
//Init init system
func Init() {
validateConf()
initDB()
}
func initDB() {
var err error
DB, err = gorm.Open("mysql", Config.MySQL.User+":"+Config.MySQL.Pass+"@tcp("+Config.MySQL.Host+":"+Config.MySQL.Port+")/"+Config.MySQL.Dbname+"?charset=utf8mb4&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
if Config.Debug {
DB.LogMode(true)
}
DB.AutoMigrate(&Proxy{})
}
func validateConf() {
if Config.Sys.VerifyDuration <= 5 {
panic("proxy verify duration (must >5 minute)")
}
}