From 1cb412a90b17fab14a323f0773bc6b347fbfbb1b Mon Sep 17 00:00:00 2001 From: bGlzdGRlcg Date: Thu, 13 Mar 2025 17:58:09 +0800 Subject: [PATCH 1/3] Add Heroku deployment configuration --- heroku.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 heroku.yml diff --git a/heroku.yml b/heroku.yml new file mode 100644 index 00000000..b0bf2c35 --- /dev/null +++ b/heroku.yml @@ -0,0 +1,6 @@ +build: + languages: + - go + +run: + web: fsb run \ No newline at end of file From 60244607fca8d28a206be635e1f69720b112697e Mon Sep 17 00:00:00 2001 From: bGlzdGRlcg Date: Thu, 13 Mar 2025 18:08:55 +0800 Subject: [PATCH 2/3] feat: add BLOCK_USERS feature --- README.md | 2 ++ config/config.go | 17 +++++++++++++++++ internal/commands/start.go | 4 ++++ internal/commands/stream.go | 4 ++++ 4 files changed, 27 insertions(+) diff --git a/README.md b/README.md index faaf3e62..16e68a7a 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,8 @@ In addition to the mandatory variables, you can also set the following optional - `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`) +- `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`) +
### Use Multiple Bots to speed up diff --git a/config/config.go b/config/config.go index 94bd059e..5b0eb013 100644 --- a/config/config.go +++ b/config/config.go @@ -21,6 +21,7 @@ import ( var ValueOf = &config{} type allowedUsers []int64 +type blockUsers []int64 func (au *allowedUsers) Decode(value string) error { if value == "" { @@ -37,6 +38,21 @@ func (au *allowedUsers) Decode(value string) error { return nil } +func (au *blockUsers) Decode(value string) error { + if value == "" { + return nil + } + ids := strings.Split(string(value), ",") + for _, id := range ids { + idInt, err := strconv.ParseInt(id, 10, 64) + if err != nil { + return err + } + *au = append(*au, idInt) + } + return nil +} + type config struct { ApiID int32 `envconfig:"API_ID" required:"true"` ApiHash string `envconfig:"API_HASH" required:"true"` @@ -50,6 +66,7 @@ type config struct { UserSession string `envconfig:"USER_SESSION"` UsePublicIP bool `envconfig:"USE_PUBLIC_IP" default:"false"` AllowedUsers allowedUsers `envconfig:"ALLOWED_USERS"` + BlockUsers blockUsers `envconfig:"BLOCK_USERS"` MultiTokens []string } diff --git a/internal/commands/start.go b/internal/commands/start.go index 429bbc19..4a1a5597 100644 --- a/internal/commands/start.go +++ b/internal/commands/start.go @@ -26,6 +26,10 @@ func start(ctx *ext.Context, u *ext.Update) error { ctx.Reply(u, "You are not allowed to use this bot.", nil) return dispatcher.EndGroups } + if len(config.ValueOf.BlockUsers) != 0 && utils.Contains(config.ValueOf.BlockUsers, chatId) { + ctx.Reply(u, "You are blocked from using this bot.", nil) + return dispatcher.EndGroups + } ctx.Reply(u, "Hi, send me any file to get a direct streamble link to that file.", nil) return dispatcher.EndGroups } diff --git a/internal/commands/stream.go b/internal/commands/stream.go index 361c5429..a48b0990 100644 --- a/internal/commands/stream.go +++ b/internal/commands/stream.go @@ -50,6 +50,10 @@ func sendLink(ctx *ext.Context, u *ext.Update) error { ctx.Reply(u, "You are not allowed to use this bot.", nil) return dispatcher.EndGroups } + if len(config.ValueOf.BlockUsers) != 0 && utils.Contains(config.ValueOf.BlockUsers, chatId) { + ctx.Reply(u, "You are blocked from using this bot.", nil) + return dispatcher.EndGroups + } supported, err := supportedMediaFilter(u.EffectiveMessage) if err != nil { return err From d69600e2388df60832e88e40a9d6c180d8b9c9b1 Mon Sep 17 00:00:00 2001 From: bGlzdGRlcg Date: Mon, 14 Apr 2025 03:35:38 +0800 Subject: [PATCH 3/3] 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 --- .gitignore | 2 ++ README.md | 64 +++++++++++++++++++------------------ config/config.go | 46 +++++++++----------------- internal/commands/start.go | 4 +-- internal/commands/stream.go | 4 +-- 5 files changed, 54 insertions(+), 66 deletions(-) diff --git a/.gitignore b/.gitignore index 7afc108d..f58430b1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ *.dll *.so *.dylib +# for linux +fsb # Test binary, built with `go test -c` *.test diff --git a/README.md b/README.md index 16e68a7a..c1cedf86 100644 --- a/README.md +++ b/README.md @@ -59,8 +59,6 @@ - - ## How to make your own ### Deploy to Heroku @@ -77,34 +75,37 @@ Press the below button to fast deploy to Heroku
### Download from releases -- 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. -- Extract the zip file to a folder. -- Create an a file named `fsb.env` and add all the variables there (see `fsb.sample.env` file for reference). -- Give the executable file permission to execute using the command `chmod +x fsb` (Not required for windows). -- Run the bot using `./fsb run` command. ( `./fsb.exe run` for windows) + +- 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. +- Extract the zip file to a folder. +- Create an a file named `fsb.env` and add all the variables there (see `fsb.sample.env` file for reference). +- Give the executable file permission to execute using the command `chmod +x fsb` (Not required for windows). +- Run the bot using `./fsb run` command. ( `./fsb.exe run` for windows)
### Run using docker-compose -- Clone the repository +- Clone the repository + ```sh git clone https://github.com/EverythingSuckz/TG-FileStreamBot cd TG-FileStreamBot ``` -- Create an a file named `fsb.env` and add all the variables there (see `fsb.sample.env` file for reference). +- Create an a file named `fsb.env` and add all the variables there (see `fsb.sample.env` file for reference). ```sh nano fsb.env ``` -- Build and run the docker-compose file +- Build and run the docker-compose file ```sh docker-compose up -d ``` - OR + +OR ```sh docker compose up -d @@ -117,6 +118,7 @@ docker compose up -d ```sh docker run --env-file fsb.env ghcr.io/everythingsuckz/fsb:latest ``` + Where `fsb.env` is the environment file containing all the variables.
@@ -141,7 +143,7 @@ nano fsb.env ``` and to stop the program, - do CTRL+C +do CTRL+C #### Windows @@ -159,7 +161,7 @@ notepad fsb.env ``` and to stop the program, - do CTRL+C +do CTRL+C ## Setting up things @@ -180,45 +182,46 @@ MULTI_TOKEN2=55838355:yourworkerbottokenhere ``` ### Required Vars + Before running the bot, you will need to set up the following mandatory variables: -- `API_ID` : This is the API ID for your Telegram account, which can be obtained from my.telegram.org. +- `API_ID` : This is the API ID for your Telegram account, which can be obtained from my.telegram.org. -- `API_HASH` : This is the API hash for your Telegram account, which can also be obtained from my.telegram.org. +- `API_HASH` : This is the API hash for your Telegram account, which can also be obtained from my.telegram.org. -- `BOT_TOKEN` : This is the bot token for the Telegram Media Streamer Bot, which can be obtained from [@BotFather](https://telegram.dog/BotFather). +- `BOT_TOKEN` : This is the bot token for the Telegram Media Streamer Bot, which can be obtained from [@BotFather](https://telegram.dog/BotFather). -- `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. +- `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. ### Optional Vars + In addition to the mandatory variables, you can also set the following optional variables: -- `PORT` : This sets the port that your webapp will listen to. The default value is 8080. +- `PORT` : This sets the port that your webapp will listen to. The default value is 8080. -- `HOST` : A Fully Qualified Domain Name if present or use your server IP. (eg. `https://example.com` or `http://14.1.154.2:8080`) +- `HOST` : A Fully Qualified Domain Name if present or use your server IP. (eg. `https://example.com` or `http://14.1.154.2:8080`) -- `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. +- `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. -- `USE_SESSION_FILE` : Use session files for worker client(s). This speeds up the worker bot startups. (default: `false`) +- `USE_SESSION_FILE` : Use session files for worker client(s). This speeds up the worker bot startups. (default: `false`) -- `USER_SESSION` : A pyrogram session string for a user bot. Used for auto adding the bots to `LOG_CHANNEL`. (default: `null`) +- `USER_SESSION` : A pyrogram session string for a user bot. Used for auto adding the bots to `LOG_CHANNEL`. (default: `null`) -- `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`) +- `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`) -- `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`) +- `USERS_ID` : A list of user IDs separated by comma (`,`). (default: `null`)
### Use Multiple Bots to speed up -> [!NOTE] -> **What it multi-client feature and what it does?**
+> [!NOTE] > **What it multi-client feature and what it does?**
> 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.
> [!NOTE] > You can add up to 50 bots since 50 is the max amount of bot admins you can set in a Telegram Channel. -To enable multi-client, generate new bot tokens and add it as your `fsb.env` with the following key names. +To enable multi-client, generate new bot tokens and add it as your `fsb.env` with the following key names. `MULTI_TOKEN1`: Add your first bot token here. @@ -267,12 +270,11 @@ Feel free to contribute to this project if you have any further ideas 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) - ## Credits -- [@celestix](https://github.com/celestix) for [gotgproto](https://github.com/celestix/gotgproto) -- [@divyam234](https://github.com/divyam234/teldrive) for his [Teldrive](https://github.com/divyam234/teldrive) Project -- [@karu](https://github.com/krau) for adding image support +- [@celestix](https://github.com/celestix) for [gotgproto](https://github.com/celestix/gotgproto) +- [@divyam234](https://github.com/divyam234/teldrive) for his [Teldrive](https://github.com/divyam234/teldrive) Project +- [@karu](https://github.com/krau) for adding image support ## Copyright diff --git a/config/config.go b/config/config.go index 5b0eb013..502353e9 100644 --- a/config/config.go +++ b/config/config.go @@ -20,25 +20,9 @@ import ( var ValueOf = &config{} -type allowedUsers []int64 -type blockUsers []int64 +type usersID []int64 -func (au *allowedUsers) Decode(value string) error { - if value == "" { - return nil - } - ids := strings.Split(string(value), ",") - for _, id := range ids { - idInt, err := strconv.ParseInt(id, 10, 64) - if err != nil { - return err - } - *au = append(*au, idInt) - } - return nil -} - -func (au *blockUsers) Decode(value string) error { +func (au *usersID) Decode(value string) error { if value == "" { return nil } @@ -54,19 +38,19 @@ func (au *blockUsers) Decode(value string) error { } type config struct { - ApiID int32 `envconfig:"API_ID" required:"true"` - ApiHash string `envconfig:"API_HASH" required:"true"` - BotToken string `envconfig:"BOT_TOKEN" required:"true"` - LogChannelID int64 `envconfig:"LOG_CHANNEL" required:"true"` - Dev bool `envconfig:"DEV" default:"false"` - Port int `envconfig:"PORT" default:"8080"` - Host string `envconfig:"HOST" default:""` - HashLength int `envconfig:"HASH_LENGTH" default:"6"` - UseSessionFile bool `envconfig:"USE_SESSION_FILE" default:"true"` - UserSession string `envconfig:"USER_SESSION"` - UsePublicIP bool `envconfig:"USE_PUBLIC_IP" default:"false"` - AllowedUsers allowedUsers `envconfig:"ALLOWED_USERS"` - BlockUsers blockUsers `envconfig:"BLOCK_USERS"` + ApiID int32 `envconfig:"API_ID" required:"true"` + ApiHash string `envconfig:"API_HASH" required:"true"` + BotToken string `envconfig:"BOT_TOKEN" required:"true"` + LogChannelID int64 `envconfig:"LOG_CHANNEL" required:"true"` + Dev bool `envconfig:"DEV" default:"false"` + Port int `envconfig:"PORT" default:"8080"` + Host string `envconfig:"HOST" default:""` + HashLength int `envconfig:"HASH_LENGTH" default:"6"` + UseSessionFile bool `envconfig:"USE_SESSION_FILE" default:"true"` + UserSession string `envconfig:"USER_SESSION"` + UsePublicIP bool `envconfig:"USE_PUBLIC_IP" default:"false"` + UsersID usersID `envconfig:"USERS_ID"` + BlockFlag bool `envconfig:"BLOCK_FLAG" default:"false"` MultiTokens []string } diff --git a/internal/commands/start.go b/internal/commands/start.go index 4a1a5597..576e68bd 100644 --- a/internal/commands/start.go +++ b/internal/commands/start.go @@ -22,11 +22,11 @@ func start(ctx *ext.Context, u *ext.Update) error { if peerChatId.Type != int(storage.TypeUser) { return dispatcher.EndGroups } - if len(config.ValueOf.AllowedUsers) != 0 && !utils.Contains(config.ValueOf.AllowedUsers, chatId) { + if !config.ValueOf.BlockFlag && len(config.ValueOf.UsersID) != 0 && !utils.Contains(config.ValueOf.UsersID, chatId) { ctx.Reply(u, "You are not allowed to use this bot.", nil) return dispatcher.EndGroups } - if len(config.ValueOf.BlockUsers) != 0 && utils.Contains(config.ValueOf.BlockUsers, chatId) { + if config.ValueOf.BlockFlag && len(config.ValueOf.UsersID) != 0 && utils.Contains(config.ValueOf.UsersID, chatId) { ctx.Reply(u, "You are blocked from using this bot.", nil) return dispatcher.EndGroups } diff --git a/internal/commands/stream.go b/internal/commands/stream.go index a48b0990..7aa85e1b 100644 --- a/internal/commands/stream.go +++ b/internal/commands/stream.go @@ -46,11 +46,11 @@ func sendLink(ctx *ext.Context, u *ext.Update) error { if peerChatId.Type != int(storage.TypeUser) { return dispatcher.EndGroups } - if len(config.ValueOf.AllowedUsers) != 0 && !utils.Contains(config.ValueOf.AllowedUsers, chatId) { + if !config.ValueOf.BlockFlag && len(config.ValueOf.UsersID) != 0 && !utils.Contains(config.ValueOf.UsersID, chatId) { ctx.Reply(u, "You are not allowed to use this bot.", nil) return dispatcher.EndGroups } - if len(config.ValueOf.BlockUsers) != 0 && utils.Contains(config.ValueOf.BlockUsers, chatId) { + if config.ValueOf.BlockFlag && len(config.ValueOf.UsersID) != 0 && utils.Contains(config.ValueOf.UsersID, chatId) { ctx.Reply(u, "You are blocked from using this bot.", nil) return dispatcher.EndGroups }