Skip to content

Commit 42038de

Browse files
feat(config): auto detect public ip
1 parent 51179cd commit 42038de

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

cmd/fsb/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func runApp(cmd *cobra.Command, args []string) {
4848
bot.StartUserBot(log)
4949
mainLogger.Info("Server started", zap.Int("port", config.ValueOf.Port))
5050
mainLogger.Info("File Stream Bot", zap.String("version", versionString))
51+
mainLogger.Sugar().Infof("Server is running at %s", config.ValueOf.Host)
5152
publicIp, err := config.GetPublicIP()
5253
if err != nil {
5354
mainLogger.Debug("Failed to get public IP", zap.Error(err))
@@ -58,7 +59,6 @@ func runApp(cmd *cobra.Command, args []string) {
5859
if err != nil {
5960
mainLogger.Sugar().Fatalln(err)
6061
}
61-
6262
}
6363

6464
func getRouter(log *zap.Logger) *gin.Engine {

config/config.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type config struct {
3131
HashLength int `envconfig:"HASH_LENGTH" default:"6"`
3232
UseSessionFile bool `envconfig:"USE_SESSION_FILE" default:"true"`
3333
UserSession string `envconfig:"USER_SESSION"`
34+
UsePublicIP bool `envconfig:"USE_PUBLIC_IP" default:"true"`
3435
MultiTokens []string
3536
}
3637

@@ -63,6 +64,7 @@ func SetFlagsFromConfig(cmd *cobra.Command) {
6364
cmd.Flags().Int("hash-length", ValueOf.HashLength, "Hash length in links")
6465
cmd.Flags().Bool("use-session-file", ValueOf.UseSessionFile, "Use session files")
6566
cmd.Flags().String("user-session", ValueOf.UserSession, "Pyrogram user session")
67+
cmd.Flags().Bool("use-public-ip", ValueOf.UsePublicIP, "Use public IP instead of local IP")
6668
cmd.Flags().String("multi-token-txt-file", "", "Multi token txt file (Not implemented)")
6769
}
6870

@@ -107,6 +109,10 @@ func (c *config) loadConfigFromArgs(log *zap.Logger, cmd *cobra.Command) {
107109
if userSession != "" {
108110
os.Setenv("USER_SESSION", userSession)
109111
}
112+
usePublicIP, _ := cmd.Flags().GetBool("use-public-ip")
113+
if usePublicIP {
114+
os.Setenv("USE_PUBLIC_IP", strconv.FormatBool(usePublicIP))
115+
}
110116
multiTokens, _ := cmd.Flags().GetString("multi-token-txt-file")
111117
if multiTokens != "" {
112118
os.Setenv("MULTI_TOKEN_TXT_FILE", multiTokens)
@@ -121,8 +127,22 @@ func (c *config) setupEnvVars(log *zap.Logger, cmd *cobra.Command) {
121127
if err != nil {
122128
log.Fatal("Error while parsing env variables", zap.Error(err))
123129
}
130+
var ipBlocked bool
131+
ip, err := getIP(c.UsePublicIP)
132+
if err != nil {
133+
log.Error("Error while getting IP", zap.Error(err))
134+
ipBlocked = true
135+
}
124136
if c.Host == "" {
125-
c.Host = "http://" + getIP() + ":" + strconv.Itoa(c.Port)
137+
c.Host = "http://" + ip + ":" + strconv.Itoa(c.Port)
138+
if c.UsePublicIP {
139+
if ipBlocked {
140+
log.Sugar().Warn("Can't get public IP, using local IP")
141+
} else {
142+
log.Sugar().Warn("You are using a public IP, please be aware of the security risks while exposing your IP to the internet.")
143+
log.Sugar().Warn("Use 'HOST' variable to set a domain name")
144+
}
145+
}
126146
log.Sugar().Info("HOST not set, automatically set to " + c.Host)
127147
}
128148
val := reflect.ValueOf(c).Elem()
@@ -153,12 +173,21 @@ func Load(log *zap.Logger, cmd *cobra.Command) {
153173
}
154174
}
155175

156-
func getIP() string {
157-
ip, err := getInternalIP()
176+
func getIP(public bool) (string, error) {
177+
var ip string
178+
var err error
179+
if public {
180+
ip, err = GetPublicIP()
181+
} else {
182+
ip, err = getInternalIP()
183+
}
184+
if ip == "" {
185+
ip = "localhost"
186+
}
158187
if err != nil {
159-
return "localhost"
188+
return "localhost", err
160189
}
161-
return ip
190+
return ip, nil
162191
}
163192

164193
// https://stackoverflow.com/a/23558495/15807350
@@ -183,7 +212,7 @@ func GetPublicIP() (string, error) {
183212
return "", err
184213
}
185214
if !checkIfIpAccessible(string(ip)) {
186-
return "", errors.New("PORT is blocked by firewall")
215+
return string(ip), errors.New("PORT is blocked by firewall")
187216
}
188217
return string(ip), nil
189218
}

0 commit comments

Comments
 (0)