@@ -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