sftpHive is a Go-based application for managing SFTP file transfers for multiple customers. It supports both scheduled and on-demand transfers, and includes a web server for monitoring job statuses and viewing logs.
sftphive/
│
├── config/
│ └── config.go
│
├── sftp/
│ ├── upload.go
│ ├── download.go
│ └── cleanup.go
│
├── static/
│ └── index.html
│
├── main/
│ └── main.go
├── server/
│ └── server.go
├── go.mod
└── configs.json
To avoid storing the SFTP password in cleartext, you can use the provided encryption utility. This utility encrypts the password and outputs the encrypted version, which you can then use in your configuration file.
- Change encryption key for the generation in:
utils/encrypt.go
Change this part
key := "aReaLlYHaxxIEncryptionKey"
- Do the same in main/main.go under the "runSFTPJob" function.
key := "mysecretencryptionkey"
-
Navigate to the directory containing the
encrypt.go
file. -
Run the following command, replacing
customer_sftp_password
with the actual SFTP password you want to encrypt:go run encrypt.go -password "customer_sftp_password"
-
The utility will output the encrypted password. Copy this encrypted password.
-
Replace the
SftpPassword
field in yourconfigs.json
with the encrypted password.
{
"customer1": {
"LocalPath": "/local/path",
"RemotePath": "/remote/path",
"SftpServer": "sftp.example.com",
"SftpPort": "22",
"SftpUserName": "username",
"SftpPassword": "ENCRYPTED_PASSWORD",
"LogFilePath": "/path/to/logs",
"ArchivePath": "/path/to/archive",
"DeleteFoldersAfterArchive": true,
"FileExtensions": "txt,csv",
"CleanupThresholdDays": 90,
"UploadRootOnly": false,
"TempRemotePath": "/temp/path",
"UseTempFolder": true,
"NewExtension": ".bak",
"DownloadEnabled": true,
"DownloadRemotePath": "/download/path",
"DownloadLocalPath": "/download/local/path",
"DownloadFileExtensions": "txt,csv",
"DownloadRootOnly": false,
"DeleteRemoteFileAfterDownload": true,
"Schedule": "@daily"
}
}
## Configuration
The `configs.json` file contains configurations for each customer. Below is an example configuration:
```json
{
"customer1": {
"LocalPath": "/local/path",
"RemotePath": "/remote/path",
"SftpServer": "sftp.example.com",
"SftpPort": "22",
"SftpUserName": "username",
"SftpPassword": "ENCRYPTED_PASSWORD",
"LogFilePath": "/path/to/logs",
"ArchivePath": "/path/to/archive",
"DeleteFoldersAfterArchive": true,
"FileExtensions": "txt,csv",
"CleanupThresholdDays": 90,
"UploadRootOnly": false,
"TempRemotePath": "/temp/path",
"UseTempFolder": true,
"NewExtension": ".bak",
"DownloadEnabled": true,
"DownloadRemotePath": "/download/path",
"DownloadLocalPath": "/download/local/path",
"DownloadFileExtensions": "txt,csv",
"DownloadRootOnly": false,
"DeleteRemoteFileAfterDownload": true,
"Schedule": "@daily"
},
"customer2": {
"LocalPath": "/local/path",
"RemotePath": "/remote/path",
"SftpServer": "sftp.example.com",
"SftpPort": "22",
"SftpUserName": "username",
"SftpPassword": "ENCRYPTED_PASSWORD",
"LogFilePath": "/path/to/logs",
"ArchivePath": "/path/to/archive",
"DeleteFoldersAfterArchive": true,
"FileExtensions": "txt,csv",
"CleanupThresholdDays": 90,
"UploadRootOnly": false,
"TempRemotePath": "/temp/path",
"UseTempFolder": true,
"NewExtension": ".bak",
"DownloadEnabled": true,
"DownloadRemotePath": "/download/path",
"DownloadLocalPath": "/download/local/path",
"DownloadFileExtensions": "txt,csv",
"DownloadRootOnly": false,
"DeleteRemoteFileAfterDownload": true,
"Schedule": "@daily"
}
}
The Schedule field in the configuration file allows you to specify when the job should run. This uses the cron format, supported by the robfig/cron package. Here are some examples:
@daily: Run once a day.
@hourly: Run once an hour.
@every 1h30m: Run every 1 hour and 30 minutes.
@every 0h15m: Run every 15 minutes.
0 30 * * * *: Run every hour at minute 30.
0 0 9 * * *: Run every day at 9 AM.
The main application handles SFTP job execution and scheduling. You can run it with or without the scheduler.
cd main
go run main.go
cd main
go run main.go --customer <customerName> --skip-scheduler
Replace with the name of the customer as specified in configs.json.
The web server provides a UI for monitoring job statuses and viewing logs.
cd server
go run server.go
Open a web browser and navigate to http://localhost:8080 to view the status of the scheduled jobs and logs.
The web interface uses Bootstrap for styling and jQuery for dynamic content updates. It provides a simple table to display job statuses and a section to view logs.
Ensure you have the following Go packages installed:
github.com/robfig/cron/v3
github.com/pkg/sftp
To build the project, you can use the Go build command:
go build -o sftphive main/main.go
go build -o sftphive-server server/server.go
Password encryption
go run utils.go -password "customer_sftp_password"
This will create two executable files, sftphive and sftphive-server, which you can run to start the main application and the web server, respectively.