Spaces:
Sleeping
Sleeping
Delete config
Browse files- config/config.go +0 -306
config/config.go
DELETED
|
@@ -1,306 +0,0 @@
|
|
| 1 |
-
package config
|
| 2 |
-
|
| 3 |
-
import (
|
| 4 |
-
"errors"
|
| 5 |
-
"io"
|
| 6 |
-
"net"
|
| 7 |
-
"net/http"
|
| 8 |
-
"os"
|
| 9 |
-
"path/filepath"
|
| 10 |
-
"reflect"
|
| 11 |
-
"regexp"
|
| 12 |
-
"strconv"
|
| 13 |
-
"strings"
|
| 14 |
-
|
| 15 |
-
"github.com/joho/godotenv"
|
| 16 |
-
"github.com/kelseyhightower/envconfig"
|
| 17 |
-
"github.com/spf13/cobra"
|
| 18 |
-
"go.uber.org/zap"
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
var ValueOf = &config{}
|
| 22 |
-
|
| 23 |
-
type allowedUsers []int64
|
| 24 |
-
|
| 25 |
-
func (au *allowedUsers) Decode(value string) error {
|
| 26 |
-
if value == "" {
|
| 27 |
-
return nil
|
| 28 |
-
}
|
| 29 |
-
ids := strings.Split(string(value), ",")
|
| 30 |
-
for _, id := range ids {
|
| 31 |
-
idInt, err := strconv.ParseInt(id, 10, 64)
|
| 32 |
-
if err != nil {
|
| 33 |
-
return err
|
| 34 |
-
}
|
| 35 |
-
*au = append(*au, idInt)
|
| 36 |
-
}
|
| 37 |
-
return nil
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
type config struct {
|
| 41 |
-
ApiID int32 `envconfig:"API_ID" required:"true"`
|
| 42 |
-
ApiHash string `envconfig:"API_HASH" required:"true"`
|
| 43 |
-
BotToken string `envconfig:"BOT_TOKEN" required:"true"`
|
| 44 |
-
LogChannelID int64 `envconfig:"LOG_CHANNEL" required:"true"`
|
| 45 |
-
Dev bool `envconfig:"DEV" default:"false"`
|
| 46 |
-
Port int `envconfig:"PORT" default:"8080"`
|
| 47 |
-
Host string `envconfig:"HOST" default:""`
|
| 48 |
-
HashLength int `envconfig:"HASH_LENGTH" default:"6"`
|
| 49 |
-
UseSessionFile bool `envconfig:"USE_SESSION_FILE" default:"true"`
|
| 50 |
-
UserSession string `envconfig:"USER_SESSION"`
|
| 51 |
-
UsePublicIP bool `envconfig:"USE_PUBLIC_IP" default:"false"`
|
| 52 |
-
AllowedUsers allowedUsers `envconfig:"ALLOWED_USERS"`
|
| 53 |
-
MultiTokens []string
|
| 54 |
-
FirebaseJSON string `envconfig:"FIREBASE_JSON"`
|
| 55 |
-
|
| 56 |
-
// stream specific config
|
| 57 |
-
StreamConcurrency int `envconfig:"STREAM_CONCURRENCY" default:"8"`
|
| 58 |
-
StreamBufferCount int `envconfig:"STREAM_BUFFER_COUNT" default:"16"`
|
| 59 |
-
StreamTimeoutSec int `envconfig:"STREAM_TIMEOUT_SEC" default:"30"`
|
| 60 |
-
StreamMaxRetries int `envconfig:"STREAM_MAX_RETRIES" default:"10"`
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
var botTokenRegex = regexp.MustCompile(`MULTI\_TOKEN\d+=(.*)`)
|
| 64 |
-
|
| 65 |
-
func (c *config) loadFromEnvFile(log *zap.Logger) {
|
| 66 |
-
envPath := filepath.Clean("fsb.env")
|
| 67 |
-
log.Sugar().Infof("Trying to load ENV vars from %s", envPath)
|
| 68 |
-
err := godotenv.Load(envPath)
|
| 69 |
-
if err != nil {
|
| 70 |
-
if os.IsNotExist(err) {
|
| 71 |
-
log.Sugar().Errorf("ENV file not found: %s", envPath)
|
| 72 |
-
log.Sugar().Info("Please create fsb.env file")
|
| 73 |
-
log.Sugar().Info("For more info, refer: https://github.com/EverythingSuckz/TG-FileStreamBot/tree/golang#setting-up-things")
|
| 74 |
-
log.Sugar().Info("Please ignore this message if you are hosting it in a service like Heroku or other alternatives.")
|
| 75 |
-
} else {
|
| 76 |
-
log.Fatal("Unknown error while parsing env file.", zap.Error(err))
|
| 77 |
-
}
|
| 78 |
-
}
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
func SetFlagsFromConfig(cmd *cobra.Command) {
|
| 82 |
-
cmd.Flags().Int32("api-id", ValueOf.ApiID, "Telegram API ID")
|
| 83 |
-
cmd.Flags().String("api-hash", ValueOf.ApiHash, "Telegram API Hash")
|
| 84 |
-
cmd.Flags().String("bot-token", ValueOf.BotToken, "Telegram Bot Token")
|
| 85 |
-
cmd.Flags().Int64("log-channel", ValueOf.LogChannelID, "Telegram Log Channel ID")
|
| 86 |
-
cmd.Flags().Bool("dev", ValueOf.Dev, "Enable development mode")
|
| 87 |
-
cmd.Flags().IntP("port", "p", ValueOf.Port, "Server port")
|
| 88 |
-
cmd.Flags().String("host", ValueOf.Host, "Server host that will be included in links")
|
| 89 |
-
cmd.Flags().Int("hash-length", ValueOf.HashLength, "Hash length in links")
|
| 90 |
-
cmd.Flags().Bool("use-session-file", ValueOf.UseSessionFile, "Use session files")
|
| 91 |
-
cmd.Flags().String("user-session", ValueOf.UserSession, "Pyrogram user session")
|
| 92 |
-
cmd.Flags().Bool("use-public-ip", ValueOf.UsePublicIP, "Use public IP instead of local IP")
|
| 93 |
-
cmd.Flags().String("multi-token-txt-file", "", "Multi token txt file (Not implemented)")
|
| 94 |
-
cmd.Flags().Int("stream-concurrency", ValueOf.StreamConcurrency, "Number of parallel block fetches")
|
| 95 |
-
cmd.Flags().Int("stream-buffer-count", ValueOf.StreamBufferCount, "Number of blocks to prefetch")
|
| 96 |
-
cmd.Flags().Int("stream-timeout-sec", ValueOf.StreamTimeoutSec, "Maximum time to wait for a single block (in seconds)")
|
| 97 |
-
cmd.Flags().Int("stream-max-retries", ValueOf.StreamMaxRetries, "Number of retry attempts for failed fetches")
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
func (c *config) loadConfigFromArgs(log *zap.Logger, cmd *cobra.Command) {
|
| 101 |
-
apiID, _ := cmd.Flags().GetInt32("api-id")
|
| 102 |
-
if apiID != 0 {
|
| 103 |
-
os.Setenv("API_ID", strconv.Itoa(int(apiID)))
|
| 104 |
-
}
|
| 105 |
-
apiHash, _ := cmd.Flags().GetString("api-hash")
|
| 106 |
-
if apiHash != "" {
|
| 107 |
-
os.Setenv("API_HASH", apiHash)
|
| 108 |
-
}
|
| 109 |
-
botToken, _ := cmd.Flags().GetString("bot-token")
|
| 110 |
-
if botToken != "" {
|
| 111 |
-
os.Setenv("BOT_TOKEN", botToken)
|
| 112 |
-
}
|
| 113 |
-
logChannelID, _ := cmd.Flags().GetString("log-channel")
|
| 114 |
-
if logChannelID != "" {
|
| 115 |
-
os.Setenv("LOG_CHANNEL", logChannelID)
|
| 116 |
-
}
|
| 117 |
-
dev, _ := cmd.Flags().GetBool("dev")
|
| 118 |
-
if dev {
|
| 119 |
-
os.Setenv("DEV", strconv.FormatBool(dev))
|
| 120 |
-
}
|
| 121 |
-
port, _ := cmd.Flags().GetInt("port")
|
| 122 |
-
if port != 0 {
|
| 123 |
-
os.Setenv("PORT", strconv.Itoa(port))
|
| 124 |
-
}
|
| 125 |
-
host, _ := cmd.Flags().GetString("host")
|
| 126 |
-
if host != "" {
|
| 127 |
-
os.Setenv("HOST", host)
|
| 128 |
-
}
|
| 129 |
-
hashLength, _ := cmd.Flags().GetInt("hash-length")
|
| 130 |
-
if hashLength != 0 {
|
| 131 |
-
os.Setenv("HASH_LENGTH", strconv.Itoa(hashLength))
|
| 132 |
-
}
|
| 133 |
-
useSessionFile, _ := cmd.Flags().GetBool("use-session-file")
|
| 134 |
-
if useSessionFile {
|
| 135 |
-
os.Setenv("USE_SESSION_FILE", strconv.FormatBool(useSessionFile))
|
| 136 |
-
}
|
| 137 |
-
userSession, _ := cmd.Flags().GetString("user-session")
|
| 138 |
-
if userSession != "" {
|
| 139 |
-
os.Setenv("USER_SESSION", userSession)
|
| 140 |
-
}
|
| 141 |
-
usePublicIP, _ := cmd.Flags().GetBool("use-public-ip")
|
| 142 |
-
if usePublicIP {
|
| 143 |
-
os.Setenv("USE_PUBLIC_IP", strconv.FormatBool(usePublicIP))
|
| 144 |
-
}
|
| 145 |
-
multiTokens, _ := cmd.Flags().GetString("multi-token-txt-file")
|
| 146 |
-
if multiTokens != "" {
|
| 147 |
-
os.Setenv("MULTI_TOKEN_TXT_FILE", multiTokens)
|
| 148 |
-
// TODO: Add support for importing tokens from a separate file
|
| 149 |
-
}
|
| 150 |
-
streamConcurrency, _ := cmd.Flags().GetInt("stream-concurrency")
|
| 151 |
-
if streamConcurrency != 0 {
|
| 152 |
-
os.Setenv("STREAM_CONCURRENCY", strconv.Itoa(streamConcurrency))
|
| 153 |
-
}
|
| 154 |
-
streamBufferCount, _ := cmd.Flags().GetInt("stream-buffer-count")
|
| 155 |
-
if streamBufferCount != 0 {
|
| 156 |
-
os.Setenv("STREAM_BUFFER_COUNT", strconv.Itoa(streamBufferCount))
|
| 157 |
-
}
|
| 158 |
-
streamTimeoutSec, _ := cmd.Flags().GetInt("stream-timeout-sec")
|
| 159 |
-
if streamTimeoutSec != 0 {
|
| 160 |
-
os.Setenv("STREAM_TIMEOUT_SEC", strconv.Itoa(streamTimeoutSec))
|
| 161 |
-
}
|
| 162 |
-
streamMaxRetries, _ := cmd.Flags().GetInt("stream-max-retries")
|
| 163 |
-
if streamMaxRetries != 0 {
|
| 164 |
-
os.Setenv("STREAM_MAX_RETRIES", strconv.Itoa(streamMaxRetries))
|
| 165 |
-
}
|
| 166 |
-
}
|
| 167 |
-
|
| 168 |
-
func (c *config) setupEnvVars(log *zap.Logger, cmd *cobra.Command) {
|
| 169 |
-
c.loadFromEnvFile(log)
|
| 170 |
-
c.loadConfigFromArgs(log, cmd)
|
| 171 |
-
err := envconfig.Process("", c)
|
| 172 |
-
if err != nil {
|
| 173 |
-
log.Fatal("Error while parsing env variables", zap.Error(err))
|
| 174 |
-
}
|
| 175 |
-
var ipBlocked bool
|
| 176 |
-
ip, err := getIP(c.UsePublicIP)
|
| 177 |
-
if err != nil {
|
| 178 |
-
log.Error("Error while getting IP", zap.Error(err))
|
| 179 |
-
ipBlocked = true
|
| 180 |
-
}
|
| 181 |
-
if c.Host == "" {
|
| 182 |
-
c.Host = "http://" + ip + ":" + strconv.Itoa(c.Port)
|
| 183 |
-
if c.UsePublicIP {
|
| 184 |
-
if ipBlocked {
|
| 185 |
-
log.Sugar().Warn("Can't get public IP, using local IP")
|
| 186 |
-
} else {
|
| 187 |
-
log.Sugar().Warn("You are using a public IP, please be aware of the security risks while exposing your IP to the internet.")
|
| 188 |
-
log.Sugar().Warn("Use 'HOST' variable to set a domain name")
|
| 189 |
-
}
|
| 190 |
-
}
|
| 191 |
-
log.Sugar().Info("HOST not set, automatically set to " + c.Host)
|
| 192 |
-
}
|
| 193 |
-
val := reflect.ValueOf(c).Elem()
|
| 194 |
-
for _, env := range os.Environ() {
|
| 195 |
-
if strings.HasPrefix(env, "MULTI_TOKEN") {
|
| 196 |
-
c.MultiTokens = append(c.MultiTokens, botTokenRegex.FindStringSubmatch(env)[1])
|
| 197 |
-
}
|
| 198 |
-
}
|
| 199 |
-
val.FieldByName("MultiTokens").Set(reflect.ValueOf(c.MultiTokens))
|
| 200 |
-
}
|
| 201 |
-
|
| 202 |
-
func Load(log *zap.Logger, cmd *cobra.Command) {
|
| 203 |
-
log = log.Named("Config")
|
| 204 |
-
defer log.Info("Loaded config")
|
| 205 |
-
ValueOf.setupEnvVars(log, cmd)
|
| 206 |
-
ValueOf.LogChannelID = int64(stripInt(log, int(ValueOf.LogChannelID)))
|
| 207 |
-
if ValueOf.HashLength == 0 {
|
| 208 |
-
log.Sugar().Info("HASH_LENGTH can't be 0, defaulting to 6")
|
| 209 |
-
ValueOf.HashLength = 6
|
| 210 |
-
}
|
| 211 |
-
if ValueOf.HashLength > 32 {
|
| 212 |
-
log.Sugar().Info("HASH_LENGTH can't be more than 32, changing to 32")
|
| 213 |
-
ValueOf.HashLength = 32
|
| 214 |
-
}
|
| 215 |
-
if ValueOf.HashLength < 5 {
|
| 216 |
-
log.Sugar().Info("HASH_LENGTH can't be less than 5, defaulting to 6")
|
| 217 |
-
ValueOf.HashLength = 6
|
| 218 |
-
}
|
| 219 |
-
if ValueOf.StreamConcurrency <= 0 {
|
| 220 |
-
log.Sugar().Info("STREAM_CONCURRENCY must be greater than 0, defaulting to 8")
|
| 221 |
-
ValueOf.StreamConcurrency = 8
|
| 222 |
-
}
|
| 223 |
-
if ValueOf.StreamBufferCount <= 0 {
|
| 224 |
-
log.Sugar().Info("STREAM_BUFFER_COUNT must be greater than 0, defaulting to 16")
|
| 225 |
-
ValueOf.StreamBufferCount = 16
|
| 226 |
-
}
|
| 227 |
-
if ValueOf.StreamTimeoutSec <= 0 {
|
| 228 |
-
log.Sugar().Info("STREAM_TIMEOUT_SEC must be greater than 0, defaulting to 30 seconds")
|
| 229 |
-
ValueOf.StreamTimeoutSec = 30
|
| 230 |
-
}
|
| 231 |
-
if ValueOf.StreamMaxRetries <= 0 {
|
| 232 |
-
log.Sugar().Info("STREAM_MAX_RETRIES must be greater than 0, defaulting to 10")
|
| 233 |
-
ValueOf.StreamMaxRetries = 10
|
| 234 |
-
}
|
| 235 |
-
}
|
| 236 |
-
|
| 237 |
-
func getIP(public bool) (string, error) {
|
| 238 |
-
var ip string
|
| 239 |
-
var err error
|
| 240 |
-
if public {
|
| 241 |
-
ip, err = GetPublicIP()
|
| 242 |
-
} else {
|
| 243 |
-
ip, err = getInternalIP()
|
| 244 |
-
}
|
| 245 |
-
if ip == "" {
|
| 246 |
-
ip = "localhost"
|
| 247 |
-
}
|
| 248 |
-
if err != nil {
|
| 249 |
-
return "localhost", err
|
| 250 |
-
}
|
| 251 |
-
return ip, nil
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
// https://stackoverflow.com/a/23558495/15807350
|
| 255 |
-
func getInternalIP() (string, error) {
|
| 256 |
-
conn, err := net.Dial("udp", "8.8.8.8:80")
|
| 257 |
-
if err != nil {
|
| 258 |
-
return "", errors.New("no internet connection")
|
| 259 |
-
}
|
| 260 |
-
defer conn.Close()
|
| 261 |
-
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
| 262 |
-
return localAddr.IP.String(), nil
|
| 263 |
-
}
|
| 264 |
-
|
| 265 |
-
func GetPublicIP() (string, error) {
|
| 266 |
-
resp, err := http.Get("https://api.ipify.org?format=text")
|
| 267 |
-
if err != nil {
|
| 268 |
-
return "", err
|
| 269 |
-
}
|
| 270 |
-
defer resp.Body.Close()
|
| 271 |
-
ip, err := io.ReadAll(resp.Body)
|
| 272 |
-
if err != nil {
|
| 273 |
-
return "", err
|
| 274 |
-
}
|
| 275 |
-
if !checkIfIpAccessible(string(ip)) {
|
| 276 |
-
return string(ip), errors.New("PORT is blocked by firewall")
|
| 277 |
-
}
|
| 278 |
-
return string(ip), nil
|
| 279 |
-
}
|
| 280 |
-
|
| 281 |
-
func checkIfIpAccessible(ip string) bool {
|
| 282 |
-
conn, err := net.Dial("tcp", ip+":80")
|
| 283 |
-
if err != nil {
|
| 284 |
-
return false
|
| 285 |
-
}
|
| 286 |
-
defer conn.Close()
|
| 287 |
-
return true
|
| 288 |
-
}
|
| 289 |
-
|
| 290 |
-
func stripInt(log *zap.Logger, a int) int {
|
| 291 |
-
strA := strconv.Itoa(abs(a))
|
| 292 |
-
lastDigits := strings.Replace(strA, "100", "", 1)
|
| 293 |
-
result, err := strconv.Atoi(lastDigits)
|
| 294 |
-
if err != nil {
|
| 295 |
-
log.Sugar().Fatalln(err)
|
| 296 |
-
return 0
|
| 297 |
-
}
|
| 298 |
-
return result
|
| 299 |
-
}
|
| 300 |
-
|
| 301 |
-
func abs(x int) int {
|
| 302 |
-
if x < 0 {
|
| 303 |
-
return -x
|
| 304 |
-
}
|
| 305 |
-
return x
|
| 306 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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