Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package settings
- import (
- "crypto/aes"
- "crypto/cipher"
- "crypto/rand"
- "encoding/json"
- "fmt"
- "io"
- "io/ioutil"
- mathrand "math/rand"
- "os"
- "time"
- )
- type urls struct {
- DanbooruURL string `json:"danbooruURL"`
- SafebooruURL string `json:"safebooruURL"`
- GelbooruURL string `json:"gelbooruURL"`
- KonachanURL string `json:"konachanURL"`
- }
- type settings struct {
- BotToken string `json:"botToken"`
- DatabaseIPAddress string `json:"databaseIPAddress"`
- DatabaseUsername string `json:"databaseUsername"`
- DatabasePassword string `json:"databasePassword"`
- DatabaseName string `json:"databaseName"`
- Addresses urls `json:"urls"`
- SettingsProcessed bool `json:"settingsProcessed"`
- }
- type startupOptions struct {
- SettingsFilePath string
- AESKey string
- }
- var BotSettings settings
- var StartupOptions startupOptions
- func init() {
- // BotSettings = settings{}
- // StartupOptions = startupOptions{}
- loadStartupOptions()
- loadSettings()
- if !BotSettings.SettingsProcessed {
- encryptSettings()
- } else {
- decryptSettings()
- }
- }
- func loadStartupOptions() {
- // TODO: Load the commandline arguments
- if StartupOptions.SettingsFilePath == "" {
- StartupOptions.SettingsFilePath = "settings.json"
- }
- }
- func loadSettings() {
- if _, err := os.Stat(StartupOptions.SettingsFilePath); os.IsNotExist(err) {
- createSettingsFile()
- }
- dat, err := ioutil.ReadFile(StartupOptions.SettingsFilePath)
- if err != nil {
- fmt.Printf("[Error reading the bot settings file: %s]\r\n", err)
- panic(err)
- }
- fmt.Println("[Loading the settings from the settings file]")
- err = json.Unmarshal([]byte(string(dat)), &BotSettings)
- if err != nil {
- fmt.Printf("[Error while unmarshaling the bot settings file: %s]\r\n", err)
- panic(err)
- }
- }
- func decryptSettings() {
- ciphertext, err := ioutil.ReadFile("myfile.data")
- // if our program was unable to read the file
- // print out the reason why it can't
- if err != nil {
- fmt.Println(err)
- }
- c, err := aes.NewCipher([]byte(StartupOptions.AESKey))
- if err != nil {
- fmt.Println(err)
- }
- gcm, err := cipher.NewGCM(c)
- if err != nil {
- fmt.Println(err)
- }
- nonceSize := gcm.NonceSize()
- }
- func decryptStr(ciphertext string, nonceSize int, gcm cipher.AEAD) string {
- if len(ciphertext) < nonceSize {
- fmt.Println(err)
- }
- nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
- plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
- if err != nil {
- fmt.Println(err)
- }
- fmt.Println(string(plaintext))
- }
- func encryptSettings() {
- // create a new random encryption key
- key := encryptGetKey()
- // generate a new aes cipher using our 32 byte long key
- c, err := aes.NewCipher(key)
- if err != nil {
- fmt.Printf("[Error creating a new cipher: %s]\r\n", err)
- panic(err)
- }
- // gcm or Galois/Counter Mode, is a mode of operation
- // for symmetric key cryptographic block ciphers
- // - https://en.wikipedia.org/wiki/Galois/Counter_Mode
- gcm, err := cipher.NewGCM(c)
- if err != nil {
- fmt.Printf("[Error creating a new cipher: %s]\r\n", err)
- panic(err)
- }
- // creates a new byte array the size of the nonce
- // which must be passed to Seal
- nonce := make([]byte, gcm.NonceSize())
- if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
- fmt.Printf("[Error while creating nonce: %s]\r\n", err)
- panic(err)
- }
- cpy := BotSettings
- cpy.BotToken = encryptStr(gcm, key, nonce, cpy.BotToken)
- cpy.DatabaseIPAddress = encryptStr(gcm, key, nonce, cpy.DatabaseIPAddress)
- cpy.DatabaseUsername = encryptStr(gcm, key, nonce, cpy.DatabaseUsername)
- cpy.DatabasePassword = encryptStr(gcm, key, nonce, cpy.DatabasePassword)
- cpy.SettingsProcessed = true
- writeOverritedSettings(cpy)
- StartupOptions.AESKey = string(key)
- fmt.Println("ENCRYPTED THE '" + StartupOptions.SettingsFilePath + "' FILE WITH KEY: '" + StartupOptions.AESKey + "'")
- }
- func encryptGetKey() []byte {
- mathrand.Seed(time.Now().UnixNano())
- key := make([]byte, 32)
- letters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`1234567890-=~!@#$%^&*()_+[]{};:,./<>?"
- for i := 0; i < len(key); i++ {
- key[i] = letters[mathrand.Intn(len(letters))]
- }
- return key
- }
- func encryptStr(gcm cipher.AEAD, key []byte, nonce []byte, str string) string {
- return string(gcm.Seal(nonce, nonce, []byte(BotSettings.BotToken), nil))
- }
- func createSettingsFile() {
- sfc := []byte(`{
- "botToken": "token",
- "databaseIPAddress": "localhost",
- "databaseUsername": "username",
- "databasePassword": "password",
- "databaseName": "kitsune",
- "urls": {
- "danbooruURL": "https://danbooru.donmai.us/",
- "safebooruURL": "https://safebooru.org/",
- "gelbooruURL": "https://gelbooru.com/",
- "konachanURL": "https://konachan.com/"
- },
- "settingsProcessed": false
- }`)
- err := ioutil.WriteFile(StartupOptions.SettingsFilePath, sfc, 0644)
- if err != nil {
- fmt.Printf("[Error creating a new settings file: %s]\r\n", err)
- panic(err)
- }
- fmt.Println("A new settings file has been created under the path: " + StartupOptions.SettingsFilePath + ". Please fill out the settings and re-run the bot.")
- os.Exit(0)
- }
- func writeOverritedSettings(s settings) {
- d, err := json.Marshal(s)
- if err != nil {
- fmt.Printf("[Error while marshaling settings: %s]\r\n", err)
- panic(err)
- }
- err = ioutil.WriteFile(StartupOptions.SettingsFilePath, d, 0644)
- if err != nil {
- fmt.Printf("[Error while saving overwrited settings: %s]\r\n", err)
- panic(err)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement