Skip to content

Commit d69600e

Browse files
committed
feat: Implement dual-mode user access control system
Add BlockFlag configuration option to control the behavior mode of the UsersID list: - When BlockFlag is false: Users in the UsersID list are allowed to use the bot - When BlockFlag is true: Users in the UsersID list are blocked from using the bot
1 parent 6024460 commit d69600e

File tree

5 files changed

+54
-66
lines changed

5 files changed

+54
-66
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*.dll
55
*.so
66
*.dylib
7+
# for linux
8+
fsb
79

810
# Test binary, built with `go test -c`
911
*.test

README.md

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
</ol>
6060
</details>
6161

62-
63-
6462
## How to make your own
6563

6664
### Deploy to Heroku
@@ -77,34 +75,37 @@ Press the below button to fast deploy to Heroku
7775
<hr>
7876

7977
### Download from releases
80-
- Head over to [releases](https://github.com/EverythingSuckz/TG-FileStreamBot/releases) tab, from the *pre release* section, download the one for your platform and architecture.
81-
- Extract the zip file to a folder.
82-
- Create an a file named `fsb.env` and add all the variables there (see `fsb.sample.env` file for reference).
83-
- Give the executable file permission to execute using the command `chmod +x fsb` (Not required for windows).
84-
- Run the bot using `./fsb run` command. ( `./fsb.exe run` for windows)
78+
79+
- Head over to [releases](https://github.com/EverythingSuckz/TG-FileStreamBot/releases) tab, from the _pre release_ section, download the one for your platform and architecture.
80+
- Extract the zip file to a folder.
81+
- Create an a file named `fsb.env` and add all the variables there (see `fsb.sample.env` file for reference).
82+
- Give the executable file permission to execute using the command `chmod +x fsb` (Not required for windows).
83+
- Run the bot using `./fsb run` command. ( `./fsb.exe run` for windows)
8584

8685
<hr>
8786

8887
### Run using docker-compose
8988

90-
- Clone the repository
89+
- Clone the repository
90+
9191
```sh
9292
git clone https://github.com/EverythingSuckz/TG-FileStreamBot
9393
cd TG-FileStreamBot
9494
```
9595

96-
- Create an a file named `fsb.env` and add all the variables there (see `fsb.sample.env` file for reference).
96+
- Create an a file named `fsb.env` and add all the variables there (see `fsb.sample.env` file for reference).
9797

9898
```sh
9999
nano fsb.env
100100
```
101101

102-
- Build and run the docker-compose file
102+
- Build and run the docker-compose file
103103

104104
```sh
105105
docker-compose up -d
106106
```
107-
OR
107+
108+
OR
108109

109110
```sh
110111
docker compose up -d
@@ -117,6 +118,7 @@ docker compose up -d
117118
```sh
118119
docker run --env-file fsb.env ghcr.io/everythingsuckz/fsb:latest
119120
```
121+
120122
Where `fsb.env` is the environment file containing all the variables.
121123

122124
<hr>
@@ -141,7 +143,7 @@ nano fsb.env
141143
```
142144

143145
and to stop the program,
144-
do <kbd>CTRL</kbd>+<kbd>C</kbd>
146+
do <kbd>CTRL</kbd>+<kbd>C</kbd>
145147

146148
#### Windows
147149

@@ -159,7 +161,7 @@ notepad fsb.env
159161
```
160162

161163
and to stop the program,
162-
do <kbd>CTRL</kbd>+<kbd>C</kbd>
164+
do <kbd>CTRL</kbd>+<kbd>C</kbd>
163165

164166
## Setting up things
165167

@@ -180,45 +182,46 @@ MULTI_TOKEN2=55838355:yourworkerbottokenhere
180182
```
181183

182184
### Required Vars
185+
183186
Before running the bot, you will need to set up the following mandatory variables:
184187

185-
- `API_ID` : This is the API ID for your Telegram account, which can be obtained from my.telegram.org.
188+
- `API_ID` : This is the API ID for your Telegram account, which can be obtained from my.telegram.org.
186189

187-
- `API_HASH` : This is the API hash for your Telegram account, which can also be obtained from my.telegram.org.
190+
- `API_HASH` : This is the API hash for your Telegram account, which can also be obtained from my.telegram.org.
188191

189-
- `BOT_TOKEN` : This is the bot token for the Telegram Media Streamer Bot, which can be obtained from [@BotFather](https://telegram.dog/BotFather).
192+
- `BOT_TOKEN` : This is the bot token for the Telegram Media Streamer Bot, which can be obtained from [@BotFather](https://telegram.dog/BotFather).
190193

191-
- `LOG_CHANNEL` : This is the channel ID for the log channel where the bot will forward media messages and store these files to make the generated direct links work. To obtain a channel ID, create a new telegram channel (public or private), post something in the channel, forward the message to [@missrose_bot](https://telegram.dog/MissRose_bot) and **reply the forwarded message** with the /id command. Copy the forwarded channel ID and paste it into the this field.
194+
- `LOG_CHANNEL` : This is the channel ID for the log channel where the bot will forward media messages and store these files to make the generated direct links work. To obtain a channel ID, create a new telegram channel (public or private), post something in the channel, forward the message to [@missrose_bot](https://telegram.dog/MissRose_bot) and **reply the forwarded message** with the /id command. Copy the forwarded channel ID and paste it into the this field.
192195

193196
### Optional Vars
197+
194198
In addition to the mandatory variables, you can also set the following optional variables:
195199

196-
- `PORT` : This sets the port that your webapp will listen to. The default value is 8080.
200+
- `PORT` : This sets the port that your webapp will listen to. The default value is 8080.
197201

198-
- `HOST` : A Fully Qualified Domain Name if present or use your server IP. (eg. `https://example.com` or `http://14.1.154.2:8080`)
202+
- `HOST` : A Fully Qualified Domain Name if present or use your server IP. (eg. `https://example.com` or `http://14.1.154.2:8080`)
199203

200-
- `HASH_LENGTH` : Custom hash length for generated URLs. The hash length must be greater than 5 and less than or equal to 32. The default value is 6.
204+
- `HASH_LENGTH` : Custom hash length for generated URLs. The hash length must be greater than 5 and less than or equal to 32. The default value is 6.
201205

202-
- `USE_SESSION_FILE` : Use session files for worker client(s). This speeds up the worker bot startups. (default: `false`)
206+
- `USE_SESSION_FILE` : Use session files for worker client(s). This speeds up the worker bot startups. (default: `false`)
203207

204-
- `USER_SESSION` : A pyrogram session string for a user bot. Used for auto adding the bots to `LOG_CHANNEL`. (default: `null`)
208+
- `USER_SESSION` : A pyrogram session string for a user bot. Used for auto adding the bots to `LOG_CHANNEL`. (default: `null`)
205209

206-
- `ALLOWED_USERS` : A list of user IDs separated by comma (`,`). If this is set, only the users in this list will be able to use the bot. (default: `null`)
210+
- `BLOCK_FLAG` : Used to determine whether to use whitelist or blacklist mode. If set to `true`, users specified in `USERS_ID` will be blocked from using the bot. If set to `false`, only users specified in `USERS_ID` will be allowed to use the bot. (default: `false`)
207211

208-
- `BLOCK_USERS` : A list of user IDs separated by comma (`,`). If this is set, users in this list will be blocked from using the bot. (default: `null`)
212+
- `USERS_ID` : A list of user IDs separated by comma (`,`). (default: `null`)
209213

210214
<hr>
211215

212216
### Use Multiple Bots to speed up
213217

214-
> [!NOTE]
215-
> **What it multi-client feature and what it does?** <br>
218+
> [!NOTE] > **What it multi-client feature and what it does?** <br>
216219
> This feature shares the Telegram API requests between worker bots to speed up download speed when many users are using the server and to avoid the flood limits that are set by Telegram. <br>
217220
218221
> [!NOTE]
219222
> You can add up to 50 bots since 50 is the max amount of bot admins you can set in a Telegram Channel.
220223
221-
To enable multi-client, generate new bot tokens and add it as your `fsb.env` with the following key names.
224+
To enable multi-client, generate new bot tokens and add it as your `fsb.env` with the following key names.
222225

223226
`MULTI_TOKEN1`: Add your first bot token here.
224227

@@ -267,12 +270,11 @@ Feel free to contribute to this project if you have any further ideas
267270

268271
You can contact either via my [Telegram Group](https://xn--r1a.click/AlteredVoid) or you can message me on [@EverythingSuckz](https://xn--r1a.click/EverythingSuckz)
269272

270-
271273
## Credits
272274

273-
- [@celestix](https://github.com/celestix) for [gotgproto](https://github.com/celestix/gotgproto)
274-
- [@divyam234](https://github.com/divyam234/teldrive) for his [Teldrive](https://github.com/divyam234/teldrive) Project
275-
- [@karu](https://github.com/krau) for adding image support
275+
- [@celestix](https://github.com/celestix) for [gotgproto](https://github.com/celestix/gotgproto)
276+
- [@divyam234](https://github.com/divyam234/teldrive) for his [Teldrive](https://github.com/divyam234/teldrive) Project
277+
- [@karu](https://github.com/krau) for adding image support
276278

277279
## Copyright
278280

config/config.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,9 @@ import (
2020

2121
var ValueOf = &config{}
2222

23-
type allowedUsers []int64
24-
type blockUsers []int64
23+
type usersID []int64
2524

26-
func (au *allowedUsers) Decode(value string) error {
27-
if value == "" {
28-
return nil
29-
}
30-
ids := strings.Split(string(value), ",")
31-
for _, id := range ids {
32-
idInt, err := strconv.ParseInt(id, 10, 64)
33-
if err != nil {
34-
return err
35-
}
36-
*au = append(*au, idInt)
37-
}
38-
return nil
39-
}
40-
41-
func (au *blockUsers) Decode(value string) error {
25+
func (au *usersID) Decode(value string) error {
4226
if value == "" {
4327
return nil
4428
}
@@ -54,19 +38,19 @@ func (au *blockUsers) Decode(value string) error {
5438
}
5539

5640
type config struct {
57-
ApiID int32 `envconfig:"API_ID" required:"true"`
58-
ApiHash string `envconfig:"API_HASH" required:"true"`
59-
BotToken string `envconfig:"BOT_TOKEN" required:"true"`
60-
LogChannelID int64 `envconfig:"LOG_CHANNEL" required:"true"`
61-
Dev bool `envconfig:"DEV" default:"false"`
62-
Port int `envconfig:"PORT" default:"8080"`
63-
Host string `envconfig:"HOST" default:""`
64-
HashLength int `envconfig:"HASH_LENGTH" default:"6"`
65-
UseSessionFile bool `envconfig:"USE_SESSION_FILE" default:"true"`
66-
UserSession string `envconfig:"USER_SESSION"`
67-
UsePublicIP bool `envconfig:"USE_PUBLIC_IP" default:"false"`
68-
AllowedUsers allowedUsers `envconfig:"ALLOWED_USERS"`
69-
BlockUsers blockUsers `envconfig:"BLOCK_USERS"`
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+
UsersID usersID `envconfig:"USERS_ID"`
53+
BlockFlag bool `envconfig:"BLOCK_FLAG" default:"false"`
7054
MultiTokens []string
7155
}
7256

internal/commands/start.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ func start(ctx *ext.Context, u *ext.Update) error {
2222
if peerChatId.Type != int(storage.TypeUser) {
2323
return dispatcher.EndGroups
2424
}
25-
if len(config.ValueOf.AllowedUsers) != 0 && !utils.Contains(config.ValueOf.AllowedUsers, chatId) {
25+
if !config.ValueOf.BlockFlag && len(config.ValueOf.UsersID) != 0 && !utils.Contains(config.ValueOf.UsersID, chatId) {
2626
ctx.Reply(u, "You are not allowed to use this bot.", nil)
2727
return dispatcher.EndGroups
2828
}
29-
if len(config.ValueOf.BlockUsers) != 0 && utils.Contains(config.ValueOf.BlockUsers, chatId) {
29+
if config.ValueOf.BlockFlag && len(config.ValueOf.UsersID) != 0 && utils.Contains(config.ValueOf.UsersID, chatId) {
3030
ctx.Reply(u, "You are blocked from using this bot.", nil)
3131
return dispatcher.EndGroups
3232
}

internal/commands/stream.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ func sendLink(ctx *ext.Context, u *ext.Update) error {
4646
if peerChatId.Type != int(storage.TypeUser) {
4747
return dispatcher.EndGroups
4848
}
49-
if len(config.ValueOf.AllowedUsers) != 0 && !utils.Contains(config.ValueOf.AllowedUsers, chatId) {
49+
if !config.ValueOf.BlockFlag && len(config.ValueOf.UsersID) != 0 && !utils.Contains(config.ValueOf.UsersID, chatId) {
5050
ctx.Reply(u, "You are not allowed to use this bot.", nil)
5151
return dispatcher.EndGroups
5252
}
53-
if len(config.ValueOf.BlockUsers) != 0 && utils.Contains(config.ValueOf.BlockUsers, chatId) {
53+
if config.ValueOf.BlockFlag && len(config.ValueOf.UsersID) != 0 && utils.Contains(config.ValueOf.UsersID, chatId) {
5454
ctx.Reply(u, "You are blocked from using this bot.", nil)
5555
return dispatcher.EndGroups
5656
}

0 commit comments

Comments
 (0)