-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
548 lines (460 loc) · 14.8 KB
/
main.go
File metadata and controls
548 lines (460 loc) · 14.8 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package main
import (
"context"
"crypto/md5"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"regexp"
"time"
"github.com/gorilla/sessions"
"github.com/joho/godotenv"
_ "github.com/mattn/go-sqlite3"
"github.com/mdp/qrterminal/v3"
"go.mau.fi/whatsmeow"
waProto "go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/store/sqlstore"
"go.mau.fi/whatsmeow/types"
waLog "go.mau.fi/whatsmeow/util/log"
"google.golang.org/protobuf/proto"
)
// Global variables for WhatsApp client and API configuration
var (
wac *whatsmeow.Client
app_host = "127.0.0.1"
app_port = "7069"
app_key = "39a473ef08c82eb0cdb13aa28c0b4c9b"
templates = template.Must(template.ParseGlob("web/*.html"))
session_store = sessions.NewCookieStore([]byte(app_key))
db_users *sql.DB
)
// Structure for receiving JSON request
type SendMessageRequest struct {
Number string `json:"number"`
Message string `json:"message"`
ApiKey string `json:"api_key"`
}
// Function to connect to WhatsApp
func ConnectToWhatsApp() (*whatsmeow.Client, error) {
// Initialize database storage
container, err := sqlstore.New(context.Background(), "sqlite3", "file:session.lock?_foreign_keys=on", waLog.Noop)
if err != nil {
return nil, fmt.Errorf("failed to initialize database: %w", err)
}
// Get the first device store
deviceStore, err := container.GetFirstDevice(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get device store: %w", err)
}
// Create a new WhatsApp client
client := whatsmeow.NewClient(deviceStore, waLog.Noop)
// If device ID is nil (not logged in), request QR code
if client.Store.ID == nil {
qrChan, err := client.GetQRChannel(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get QR channel: %w", err)
}
// Try connecting to WhatsApp
err = client.Connect()
if err != nil {
return nil, fmt.Errorf("failed to connect to WhatsApp: %w", err)
}
// Display QR code for login
for evt := range qrChan {
if evt.Event == "code" {
log.Println("Please scan the following QR code to connect to WhatsApp:")
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
} else {
log.Printf("Login event: %s", evt.Event)
if evt.Event == "timeout" {
os.Exit(1)
}
}
}
} else {
// If already logged in, connect directly
err := client.Connect()
if err != nil {
return nil, fmt.Errorf("failed to connect to WhatsApp: %w", err)
}
}
return client, nil
}
// Function to generate table for storing user data
func createTable() {
query := `
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE,
password TEXT,
api_key TEXT
);
`
_, err := db_users.Exec(query)
if err != nil {
log.Fatal(err)
}
}
// Function to create a default admin user if it doesn't exist
func createDefaultAdmin() {
var count int
db_users.QueryRow(`SELECT COUNT(*) FROM users WHERE username = 'admin'`).Scan(&count)
if count == 0 {
apiKey := generateApiKey()
password := hashMD5("admin")
_, err := db_users.Exec(`INSERT INTO users (username, password, api_key) VALUES (?, ?, ?)`, "admin", password, apiKey)
if err != nil {
log.Fatal(err)
}
log.Println("Default user created username: admin | password: admin")
}
}
// Function to generate a random API key
func generateApiKey() string {
now := time.Now().String()
return hashMD5(now)
}
// Function to generate MD5 hash
func hashMD5(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
}
// Function to validate phone numbers (international format without '+')
func IsValidPhoneNumber(phone string) bool {
re := regexp.MustCompile(`^(\d{1,3})(\d{6,14})$`)
return re.MatchString(phone)
}
// Function to handle home page
func HomeHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w, "home.html", nil)
}
// Function to handle register page
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
session, _ := session_store.Get(r, "session")
if session.Values["user_id"] != nil {
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
return
}
if r.Method == http.MethodPost {
username := r.FormValue("username")
password := r.FormValue("password")
if username == "" || password == "" {
data := struct {
Error string
}{
Error: "username and password cannot be empty",
}
templates.ExecuteTemplate(w, "register.html", data)
return
}
var exists int
err := db_users.QueryRow(`SELECT COUNT(*) FROM users WHERE username = ?`, username).Scan(&exists)
if err != nil {
data := struct {
Error string
}{
Error: "internal server error",
}
templates.ExecuteTemplate(w, "register.html", data)
return
}
if exists > 0 {
data := struct {
Error string
}{
Error: "username already exists",
}
templates.ExecuteTemplate(w, "register.html", data)
return
}
hashedPassword := hashMD5(password)
apiKey := hashMD5(fmt.Sprintf("%d", time.Now().UnixNano()))
_, err = db_users.Exec(`INSERT INTO users (username, password, api_key) VALUES (?, ?, ?)`, username, hashedPassword, apiKey)
if err != nil {
http.Error(w, "Gagal register", http.StatusInternalServerError)
return
}
var id int
db_users.QueryRow(`SELECT id FROM users WHERE username = ?`, username).Scan(&id)
session.Values["user_id"] = id
session.Save(r, w)
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
return
}
templates.ExecuteTemplate(w, "register.html", nil)
}
// Function to handle login page
func LoginHandler(w http.ResponseWriter, r *http.Request) {
session, _ := session_store.Get(r, "session")
if session.Values["user_id"] != nil {
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
return
}
if r.Method == http.MethodPost {
username := r.FormValue("username")
password := r.FormValue("password")
hashedPassword := hashMD5(password)
if username == "" || password == "" {
data := struct {
Error string
}{
Error: "username and password cannot be empty",
}
templates.ExecuteTemplate(w, "login.html", data)
return
}
var id int
var dbPassword string
err := db_users.QueryRow(`SELECT id, password FROM users WHERE username = ?`, username).Scan(&id, &dbPassword)
if err != nil || dbPassword != hashedPassword {
data := struct {
Error string
}{
Error: "Invalid username or password",
}
templates.ExecuteTemplate(w, "login.html", data)
return
}
session.Values["user_id"] = id
session.Save(r, w)
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
return
}
templates.ExecuteTemplate(w, "login.html", nil)
}
// Function to handle logout
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
session, _ := session_store.Get(r, "session")
session.Values["user_id"] = nil
session.Options.MaxAge = -1
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
// Function to handle dashboard page
func DashboardHandler(w http.ResponseWriter, r *http.Request) {
session, _ := session_store.Get(r, "session")
userID, ok := session.Values["user_id"].(int)
if !ok || userID == 0 {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
var apiKey string
err := db_users.QueryRow(`SELECT api_key FROM users WHERE id = ?`, userID).Scan(&apiKey)
if err != nil {
http.Error(w, "User not found", http.StatusNotFound)
return
}
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
fullURL := fmt.Sprintf("%s://%s", scheme, r.Host)
data := struct {
Url string
ApiKey string
}{
Url: fullURL,
ApiKey: apiKey,
}
templates.ExecuteTemplate(w, "dashboard.html", data)
}
// Function to handle change credentials
func ChangeCredentialsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
return
}
session, _ := session_store.Get(r, "session")
userID, ok := session.Values["user_id"].(int)
if !ok {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
newUsername := r.FormValue("newUsername")
newPassword := r.FormValue("newPassword")
if newUsername == "" || newPassword == "" {
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
return
}
var existingID int
err := db_users.QueryRow("SELECT id FROM users WHERE username = ?", newUsername).Scan(&existingID)
if err != sql.ErrNoRows && existingID != userID {
http.Error(w, "Username already taken", http.StatusBadRequest)
return
}
hashedPassword := fmt.Sprintf("%x", md5.Sum([]byte(newPassword)))
_, err = db_users.Exec("UPDATE users SET username = ?, password = ? WHERE id = ?", newUsername, hashedPassword, userID)
if err != nil {
http.Error(w, "Failed to update credentials", http.StatusInternalServerError)
return
}
session.Options.MaxAge = -1
session.Save(r, w)
http.Redirect(w, r, "/login", http.StatusSeeOther)
}
// Function to handle WhatsApp message sending
func SendMessageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
log.Printf("Method %s not allowed on /send-message", r.Method)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "failed",
"message": "Method " + r.Method + " is not allowed",
})
return
}
var request SendMessageRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&request)
if err != nil {
log.Printf("Failed to parse JSON body: %v", err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "failed",
"message": "Invalid JSON body",
})
return
}
if request.Number == "" || request.Message == "" || request.ApiKey == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "failed",
"message": "Number, message, and token cannot be empty",
})
return
}
var apiKey string
api_err := db_users.QueryRow(`SELECT api_key FROM users WHERE api_key = ?`, request.ApiKey).Scan(&apiKey)
if api_err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "failed",
"message": "Invalid api_key",
})
return
}
if !IsValidPhoneNumber(request.Number) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "failed",
"message": "Invalid phone number format",
})
return
}
if wac == nil {
log.Println("WhatsApp is not connected")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "failed",
"message": "WhatsApp is not connected",
})
return
}
jid := types.JID{
User: request.Number,
Server: types.DefaultUserServer,
}
message := &waProto.Message{
Conversation: proto.String(request.Message),
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resultChan := make(chan error, 1)
go func() {
_, err := wac.SendMessage(ctx, jid, message)
resultChan <- err
}()
select {
case err := <-resultChan:
if err != nil {
log.Printf("Failed to send message to %s: %v", request.Number, err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "failed",
"message": "Failed to send message: " + err.Error(),
})
return
}
case <-ctx.Done():
log.Printf("Timeout sending message to %s", request.Number)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusRequestTimeout)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "failed",
"message": "Failed to send message: timeout",
})
return
}
log.Printf("Message sent to +%s", request.Number)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "success",
"message": "Message successfully sent to " + request.Number,
})
}
func main() {
log.Println("Starting WhatsApp Message Sender Service")
err := godotenv.Load("kirimkan.conf")
if err != nil {
log.Printf("Failed to read configuration file: %v", err)
os.Exit(1)
}
app_host = os.Getenv("API_HOST")
app_port = os.Getenv("API_PORT")
app_key = os.Getenv("API_KEY")
log.Println("Configuration loaded:")
log.Printf("API_HOST: %s", app_host)
log.Printf("API_PORT: %s", app_port)
session_store = sessions.NewCookieStore([]byte(app_key))
var errWA error
wac, errWA = ConnectToWhatsApp()
if errWA != nil {
log.Printf("Failed to connect to WhatsApp: %v", errWA)
return
} else {
log.Println("Connected to WhatsApp")
}
defer wac.Disconnect()
var db_user_err error
db_users, db_user_err = sql.Open("sqlite3", "usersdb.sqlite")
if db_user_err != nil {
log.Printf("Failed to connect to database: %v", db_user_err)
return
}
defer db_users.Close()
createTable()
createDefaultAdmin()
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/", HomeHandler)
http.HandleFunc("/register", RegisterHandler)
http.HandleFunc("/login", LoginHandler)
http.HandleFunc("/logout", LogoutHandler)
http.HandleFunc("/dashboard", DashboardHandler)
http.HandleFunc("/change-credentials", ChangeCredentialsHandler)
http.HandleFunc("/send-message", SendMessageHandler)
fmt.Print(`
██╗ ██╗ ██╗ ██████╗ ██╗ ███╗ ███╗ ██╗ ██╗ █████╗ ███╗ ██╗
██║ ██╔╝ ██║ ██╔══██╗ ██║ ████╗ ████║ ██║ ██╔╝ ██╔══██╗ ████╗ ██║
█████╔╝ ██║ ██████╔╝ ██║ ██╔████╔██║ █████╔╝ ███████║ ██╔██╗ ██║
██╔═██╗ ██║ ██╔══██╗ ██║ ██║╚██╔╝██║ ██╔═██╗ ██╔══██║ ██║╚██╗██║
██║ ██╗ ██║ ██║ ██║ ██║ ██║ ╚═╝ ██║ ██║ ██╗ ██║ ██║ ██║ ╚████║
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝
`)
addr := fmt.Sprintf("%s:%s", app_host, app_port)
log.Printf("Server is running on http://%s", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}