From b78aec6eef883cfa82b4e66d5fda2ace6baf5f82 Mon Sep 17 00:00:00 2001 From: rxdn Date: Sun, 26 Jul 2020 17:06:28 +0100 Subject: [PATCH] use envvars --- app/http/middleware/verifywhitelabel.go | 4 +- config/config.go | 78 +++++++- envvars.md | 24 +++ utils/discord/auth.go | 238 ++++++++++++------------ 4 files changed, 214 insertions(+), 130 deletions(-) create mode 100644 envvars.md diff --git a/app/http/middleware/verifywhitelabel.go b/app/http/middleware/verifywhitelabel.go index 3f09e9e..d0a1b7b 100644 --- a/app/http/middleware/verifywhitelabel.go +++ b/app/http/middleware/verifywhitelabel.go @@ -15,8 +15,8 @@ func VerifyWhitelabel(isApi bool) func(ctx *gin.Context) { if rpc.PremiumClient.GetTierByUser(userId, false) < premium.Whitelabel { var isForced bool - for _, forced := range config.Conf.ForceWhitelabel { - if forced == userId { + for _, id := range config.Conf.Admins { + if id == userId { isForced = true break } diff --git a/config/config.go b/config/config.go index 35870a1..c38765d 100644 --- a/config/config.go +++ b/config/config.go @@ -1,8 +1,10 @@ package config import ( - "github.com/BurntSushi/toml" - "io/ioutil" + "github.com/TicketsBot/common/sentry" + "os" + "strconv" + "strings" ) type ( @@ -37,7 +39,7 @@ type ( } Oauth struct { - Id int64 + Id uint64 Secret string RedirectUri string } @@ -71,13 +73,71 @@ var ( ) func LoadConfig() { - raw, err := ioutil.ReadFile("config.toml") - if err != nil { - panic(err) + var admins []uint64 + for _, id := range strings.Split(os.Getenv("ADMINS"), ",") { + if parsed, err := strconv.ParseUint(id, 10, 64); err == nil { + admins = append(admins, parsed) + } else { + sentry.Error(err) + } } - _, err = toml.Decode(string(raw), &Conf) - if err != nil { - panic(err) + var forcedWhitelabel []uint64 + for _, id := range strings.Split(os.Getenv("FORCED_WHITELABEL"), ",") { + if parsed, err := strconv.ParseUint(id, 10, 64); err == nil { + forcedWhitelabel = append(forcedWhitelabel, parsed) + } else { + sentry.Error(err) + } + } + + rateLimitWindow, _ := strconv.Atoi(os.Getenv("RATELIMIT_WINDOW")) + rateLimitMax, _ := strconv.Atoi(os.Getenv("RATELIMIT_MAX")) + sessionThreads, _ := strconv.Atoi(os.Getenv("SESSION_DB_THREADS")) + oauthId, _ := strconv.ParseUint(os.Getenv("OAUTH_ID"), 10, 64) + redisPort, _ := strconv.Atoi(os.Getenv("REDIS_PORT")) + redisThreads, _ := strconv.Atoi(os.Getenv("REDIS_THREADS")) + + Conf = Config{ + Admins: admins, + ForceWhitelabel: forcedWhitelabel, + Server: Server{ + Host: os.Getenv("SERVER_ADDR"), + BaseUrl: os.Getenv("BASE_URL"), + MainSite: os.Getenv("MAIN_SITE"), + Ratelimit: Ratelimit{ + Window: rateLimitWindow, + Max: rateLimitMax, + }, + Session: Session{ + Threads: sessionThreads, + Secret: os.Getenv("SESSION_SECRET"), + }, + Secret: os.Getenv("JWT_SECRET"), + }, + Oauth: Oauth{ + Id: oauthId, + Secret: os.Getenv("OAUTH_SECRET"), + RedirectUri: os.Getenv("OAUTH_REDIRECT_URI"), + }, + Database: Database{ + Uri: os.Getenv("DATABASE_URI"), + }, + Bot: Bot{ + Token: os.Getenv("BOT_TOKEN"), + PremiumLookupProxyUrl: os.Getenv("PREMIUM_PROXY_URL"), + PremiumLookupProxyKey: os.Getenv("PREMIUM_PROXY_KEY"), + ObjectStore: os.Getenv("LOG_ARCHIVER_URL"), + AesKey: os.Getenv("LOG_AES_KEY"), + }, + Redis: Redis{ + Host: os.Getenv("REDIS_HOST"), + Port: redisPort, + Password: os.Getenv("REDIS_PORT"), + Threads: redisThreads, + }, + Cache: Cache{ + Uri: os.Getenv("CACHE_URI"), + }, } } diff --git a/envvars.md b/envvars.md new file mode 100644 index 0000000..215d8e0 --- /dev/null +++ b/envvars.md @@ -0,0 +1,24 @@ +- ADMINS +- FORCED_WHITELABEL +- SERVER_ADDR +- BASE_URL +- MAIN_SITE +- RATELIMIT_WINDOW +- RATELIMIT_MAX +- SESSION_DB_THREADS +- SESSION_SECRET +- JWT_SECRET +- OAUTH_ID +- OAUTH_SECRET +- OAUTH_REDIRECT_URI +- DATABASE_URI +- BOT_TOKEN +- PREMIUM_PROXY_URL +- PREMIUM_PROXY_KEY +- LOG_ARCHIVER_URL +- LOG_AES_KEY +- REDIS_HOST +- REDIS_PORT +- REDIS_PASSWORD +- REDIS_THREADS +- CACHE_URI \ No newline at end of file diff --git a/utils/discord/auth.go b/utils/discord/auth.go index e2a2157..5edf0b8 100644 --- a/utils/discord/auth.go +++ b/utils/discord/auth.go @@ -1,119 +1,119 @@ -package discord - -import ( - "bytes" - "encoding/json" - "github.com/TicketsBot/GoPanel/config" - "github.com/pasztorpisti/qs" - "io/ioutil" - "net/http" - "strconv" - "time" -) - -type ( - TokenData struct { - ClientId string `qs:"client_id"` - ClientSecret string `qs:"client_secret"` - GrantType string `qs:"grant_type"` - Code string `qs:"code"` - RedirectUri string `qs:"redirect_uri"` - Scope string `qs:"scope"` - } - - RefreshData struct { - ClientId string `qs:"client_id"` - ClientSecret string `qs:"client_secret"` - GrantType string `qs:"grant_type"` - RefreshToken string `qs:"refresh_token"` - RedirectUri string `qs:"redirect_uri"` - Scope string `qs:"scope"` - } - - TokenResponse struct { - AccessToken string `json:"access_token"` - TokenType string `json:"token_type"` - ExpiresIn int `json:"expires_in"` - RefreshToken string `json:"refresh_token"` - Scope string `json:"scope"` - } -) - -const TokenEndpoint = "https://discordapp.com/api/oauth2/token" - -func AccessToken(code string) (TokenResponse, error) { - data := TokenData{ - ClientId: strconv.Itoa(int(config.Conf.Oauth.Id)), - ClientSecret: config.Conf.Oauth.Secret, - GrantType: "authorization_code", - Code: code, - RedirectUri: config.Conf.Oauth.RedirectUri, - Scope: "identify guilds", - } - - res, err := tokenPost(data) - if err != nil { - return TokenResponse{}, err - } - - var unmarshalled TokenResponse - if err = json.Unmarshal(res, &unmarshalled); err != nil { - return TokenResponse{}, err - } - - return unmarshalled, nil -} - -func RefreshToken(refreshToken string) (TokenResponse, error) { - data := RefreshData{ - ClientId: strconv.Itoa(int(config.Conf.Oauth.Id)), - ClientSecret: config.Conf.Oauth.Secret, - GrantType: "refresh_token", - RefreshToken: refreshToken, - RedirectUri: config.Conf.Oauth.RedirectUri, - Scope: "identify guilds", - } - - res, err := tokenPost(data) - if err != nil { - return TokenResponse{}, err - } - - var unmarshalled TokenResponse - if err = json.Unmarshal(res, &unmarshalled); err != nil { - return TokenResponse{}, err - } - - return unmarshalled, nil -} - -func tokenPost(body ...interface{}) ([]byte, error) { - str, err := qs.Marshal(body[0]) - if err != nil { - return nil, err - } - encoded := []byte(str) - - req, err := http.NewRequest("POST", TokenEndpoint, bytes.NewBuffer([]byte(encoded))) - if err != nil { - return nil, err - } - - req.Header.Set("Content-Type", string(ApplicationFormUrlEncoded)) - - client := &http.Client{} - client.Timeout = 3 * time.Second - - res, err := client.Do(req) - if err != nil { - return nil, err - } - - defer res.Body.Close() - content, err := ioutil.ReadAll(res.Body) - if err != nil { - return nil, err - } - - return content, nil -} +package discord + +import ( + "bytes" + "encoding/json" + "github.com/TicketsBot/GoPanel/config" + "github.com/pasztorpisti/qs" + "io/ioutil" + "net/http" + "strconv" + "time" +) + +type ( + TokenData struct { + ClientId string `qs:"client_id"` + ClientSecret string `qs:"client_secret"` + GrantType string `qs:"grant_type"` + Code string `qs:"code"` + RedirectUri string `qs:"redirect_uri"` + Scope string `qs:"scope"` + } + + RefreshData struct { + ClientId string `qs:"client_id"` + ClientSecret string `qs:"client_secret"` + GrantType string `qs:"grant_type"` + RefreshToken string `qs:"refresh_token"` + RedirectUri string `qs:"redirect_uri"` + Scope string `qs:"scope"` + } + + TokenResponse struct { + AccessToken string `json:"access_token"` + TokenType string `json:"token_type"` + ExpiresIn int `json:"expires_in"` + RefreshToken string `json:"refresh_token"` + Scope string `json:"scope"` + } +) + +const TokenEndpoint = "https://discordapp.com/api/oauth2/token" + +func AccessToken(code string) (TokenResponse, error) { + data := TokenData{ + ClientId: strconv.FormatUint(config.Conf.Oauth.Id, 10), + ClientSecret: config.Conf.Oauth.Secret, + GrantType: "authorization_code", + Code: code, + RedirectUri: config.Conf.Oauth.RedirectUri, + Scope: "identify guilds", + } + + res, err := tokenPost(data) + if err != nil { + return TokenResponse{}, err + } + + var unmarshalled TokenResponse + if err = json.Unmarshal(res, &unmarshalled); err != nil { + return TokenResponse{}, err + } + + return unmarshalled, nil +} + +func RefreshToken(refreshToken string) (TokenResponse, error) { + data := RefreshData{ + ClientId: strconv.FormatUint(config.Conf.Oauth.Id, 10), + ClientSecret: config.Conf.Oauth.Secret, + GrantType: "refresh_token", + RefreshToken: refreshToken, + RedirectUri: config.Conf.Oauth.RedirectUri, + Scope: "identify guilds", + } + + res, err := tokenPost(data) + if err != nil { + return TokenResponse{}, err + } + + var unmarshalled TokenResponse + if err = json.Unmarshal(res, &unmarshalled); err != nil { + return TokenResponse{}, err + } + + return unmarshalled, nil +} + +func tokenPost(body ...interface{}) ([]byte, error) { + str, err := qs.Marshal(body[0]) + if err != nil { + return nil, err + } + encoded := []byte(str) + + req, err := http.NewRequest("POST", TokenEndpoint, bytes.NewBuffer([]byte(encoded))) + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", string(ApplicationFormUrlEncoded)) + + client := &http.Client{} + client.Timeout = 3 * time.Second + + res, err := client.Do(req) + if err != nil { + return nil, err + } + + defer res.Body.Close() + content, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + + return content, nil +}