This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.go
229 lines (193 loc) · 4.72 KB
/
funcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/raifpy/Go/raiFile"
input "github.com/tcnksm/go-input"
"google.golang.org/api/drive/v3"
)
func ginLog(g *gin.Context) {
if varJSON.ShowLogs {
log.Println(g.Request.URL.Path, g.ClientIP())
}
if varJSON.SaveLogs {
raiFile.WriteFileLines(saveLogsPath, fmt.Sprintf("Time: %s\nIp: %s\nPath: %s\n", time.Now().String(), g.ClientIP(), g.Request.URL.String()))
}
}
func nLog(veri string) {
if varJSON.ShowLogs {
log.Println(veri)
}
if varJSON.SaveLogs {
raiFile.WriteFileLines(saveLogsPath, fmt.Sprintf("Time: %s\nLog: %s\n\n", time.Now().String(), veri))
}
}
func to32digit(passStr string) []byte {
pass := []byte(passStr)
if len(pass) == 32 {
return pass
}
if len(pass) < 32 {
var newPass = make([]byte, 32)
copy(newPass, pass)
for i := len(pass); i < 32; i++ {
newPass[i] = 5
}
return newPass
}
if len(pass) > 32 {
return pass[:32]
}
return pass
}
func getHashInput() []byte {
if _, err := os.Stat("token.json"); os.IsNotExist(err) {
hashTmp, err := inputUI.Ask("Create a password [Enter-use default]", &input.Options{HideDefault: true, Default: defaultHash})
if err != nil {
log.Fatalln(err)
}
if hashTmp != defaultHash {
return to32digit(hashTmp)
}
f, err := os.Create(".default")
if err != nil {
nLog(err.Error() + "\nCloudn't create .default\nYOU NEED CREATE MANUEL\n\tType \"touch .default\"")
} else {
f.Close()
}
//firstOpen = true
return to32digit(defaultHash)
}
// token.json mevcut ise;
if _, err := os.Stat(".default"); err == nil {
return to32digit(defaultHash)
}
hashTmp, err := inputUI.Ask("Give me pass?", &input.Options{HideDefault: true, Loop: true, Required: true, Hide: true})
if err != nil {
log.Fatalln(err)
}
return to32digit(hashTmp)
}
func toStringJSON(veri interface{}) string {
ğ, err := json.Marshal(veri)
if err != nil {
log.Println(err)
return ""
}
ğğ := string(ğ)
ğ = nil
return ğğ
}
func isInList(key string, list []string) bool {
for _, value := range list {
if value == key {
return true
}
}
return false
}
func filesToTemplateHTMLTdStructArray(files []*drive.File) []templateHTMLTdStruct {
var veri = make([]templateHTMLTdStruct, len(files))
for index, value := range files {
mime := formatMimeType(value.MimeType)
var path string
if mime == "folder" {
path = "/d"
} else {
path = "/f"
}
veri[index] = templateHTMLTdStruct{
Href: path + "?id=" + value.Id,
Name: value.Name,
Mime: mime,
Size: fmt.Sprint(value.Size),
}
}
return veri
}
func formatMimeType(mimetype string) string {
if mimetype == googleDriveFolderMime {
return "folder"
}
spi := strings.Split(mimetype, "/")
if len(spi) == 1 {
return spi[0]
}
return spi[1]
}
func getDownloadIPMapItem(key string) int {
value, ok := downloadIPMap.Load(key)
if !ok {
return -1
}
return value.(int)
}
func addDownloadIPMapItem(key string, digit int) {
downloadIPMap.Store(key, digit)
}
func deleteDownloadIPMapItem(key string) {
downloadIPMap.Delete(key)
}
func getFileDriveStorageValue(id string) (*drive.File, bool) {
fileArrayInterface, ok := fileDriveStorageMap.Load(id)
if !ok {
return nil, false
}
return fileArrayInterface.(*drive.File), true
}
func getDirDriveStorageValue(id string) ([]*drive.File, bool) {
fileArrayInterface, ok := dirDriveStorageMap.Load(id)
if !ok {
return nil, false
}
return fileArrayInterface.([]*drive.File), true
}
func addFileDriveStoregeValue(id string, files *drive.File) {
fileDriveStorageMap.Store(id, files)
}
func addDirDriveStoregeValue(id string, files []*drive.File) {
dirDriveStorageMap.Store(id, files)
}
func getDailyIPLimitMapValue(ip string) int {
value, ok := dailyIPLimitMap.Load(ip)
if !ok {
return 0
}
return value.(int)
}
func addDailyIPLimitMapValue(ip string) {
dailyIPLimitMap.Store(ip, getDailyIPLimitMapValue(ip)+1)
}
func deleteDailyIPLimitMapSync() {
for range time.NewTicker(time.Hour * 24).C {
dailyIPLimitMap = sync.Map{}
}
}
func checkIPAddrOverDailyRequest(ip string) bool {
if getDailyIPLimitMapValue(ip) >= varJSON.DailyIPLimit {
nLog(ip + " blocked: daily limit")
return true
}
addDailyIPLimitMapValue(ip)
return false
}
func blockDownloadAfterDownload(ip string) bool {
_, ok := blockDownloadAfterDownloadMap.Load(ip)
if !ok {
//nLog(ip + " download block: after download block config")
blockDownloadAfterDownloadMap.Store(ip, nil)
go deleteBlockDownloadAfterDownload(ip)
//nLog(ip + " can download files now: unblocked")
}
return ok
}
func deleteBlockDownloadAfterDownload(ip string) {
time.Sleep(time.Minute * time.Duration(varJSON.BlockDownloadAfterDownloadMin))
blockDownloadAfterDownloadMap.Delete(ip)
}