Skip to content

Commit a80741e

Browse files
Added checkbox FwMark in QRCode generation (ngoduykhanh#260)
1 parent 9d2dd71 commit a80741e

File tree

6 files changed

+85
-30
lines changed

6 files changed

+85
-30
lines changed

handler/routes.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,15 @@ func GetClient(db store.IStore) echo.HandlerFunc {
138138
return func(c echo.Context) error {
139139

140140
clientID := c.Param("id")
141-
clientData, err := db.GetClientByID(clientID, true)
141+
qrCodeIncludeFwMark := c.QueryParam("qrCodeIncludeFwMark")
142+
qrCodeSettings := model.QRCodeSettings{
143+
Enabled: true,
144+
IncludeDNS: true,
145+
IncludeFwMark: qrCodeIncludeFwMark == "true",
146+
IncludeMTU: true,
147+
}
148+
149+
clientData, err := db.GetClientByID(clientID, qrCodeSettings)
142150
if err != nil {
143151
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Client not found"})
144152
}
@@ -257,7 +265,13 @@ func EmailClient(db store.IStore, mailer emailer.Emailer, emailSubject, emailCon
257265
c.Bind(&payload)
258266
// TODO validate email
259267

260-
clientData, err := db.GetClientByID(payload.ID, true)
268+
qrCodeSettings := model.QRCodeSettings{
269+
Enabled: true,
270+
IncludeDNS: true,
271+
IncludeFwMark: true,
272+
IncludeMTU: true,
273+
}
274+
clientData, err := db.GetClientByID(payload.ID, qrCodeSettings)
261275
if err != nil {
262276
log.Errorf("Cannot generate client id %s config file for downloading: %v", payload.ID, err)
263277
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Client not found"})
@@ -304,7 +318,7 @@ func UpdateClient(db store.IStore) echo.HandlerFunc {
304318
c.Bind(&_client)
305319

306320
// validate client existence
307-
clientData, err := db.GetClientByID(_client.ID, false)
321+
clientData, err := db.GetClientByID(_client.ID, model.QRCodeSettings{Enabled: false})
308322
if err != nil {
309323
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Client not found"})
310324
}
@@ -368,7 +382,7 @@ func SetClientStatus(db store.IStore) echo.HandlerFunc {
368382
clientID := data["id"].(string)
369383
status := data["status"].(bool)
370384

371-
clientdata, err := db.GetClientByID(clientID, false)
385+
clientdata, err := db.GetClientByID(clientID, model.QRCodeSettings{Enabled: false})
372386
if err != nil {
373387
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, err.Error()})
374388
}
@@ -393,7 +407,7 @@ func DownloadClient(db store.IStore) echo.HandlerFunc {
393407
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Missing clientid parameter"})
394408
}
395409

396-
clientData, err := db.GetClientByID(clientID, false)
410+
clientData, err := db.GetClientByID(clientID, model.QRCodeSettings{Enabled: false})
397411
if err != nil {
398412
log.Errorf("Cannot generate client id %s config file for downloading: %v", clientID, err)
399413
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Client not found"})

model/client.go

+7
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ type ClientData struct {
2626
Client *Client
2727
QRCode string
2828
}
29+
30+
type QRCodeSettings struct {
31+
Enabled bool
32+
IncludeDNS bool
33+
IncludeFwMark bool
34+
IncludeMTU bool
35+
}

store/jsondb/jsondb.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (o *JsonDB) GetClients(hasQRCode bool) ([]model.ClientData, error) {
194194
return clients, nil
195195
}
196196

197-
func (o *JsonDB) GetClientByID(clientID string, hasQRCode bool) (model.ClientData, error) {
197+
func (o *JsonDB) GetClientByID(clientID string, qrCodeSettings model.QRCodeSettings) (model.ClientData, error) {
198198
client := model.Client{}
199199
clientData := model.ClientData{}
200200

@@ -204,9 +204,17 @@ func (o *JsonDB) GetClientByID(clientID string, hasQRCode bool) (model.ClientDat
204204
}
205205

206206
// generate client qrcode image in base64
207-
if hasQRCode && client.PrivateKey != "" {
207+
if qrCodeSettings.Enabled && client.PrivateKey != "" {
208208
server, _ := o.GetServer()
209209
globalSettings, _ := o.GetGlobalSettings()
210+
client := client
211+
client.UseServerDNS = qrCodeSettings.IncludeDNS
212+
if !qrCodeSettings.IncludeMTU {
213+
globalSettings.MTU = 0
214+
}
215+
if !qrCodeSettings.IncludeFwMark {
216+
globalSettings.ForwardMark = ""
217+
}
210218

211219
png, err := qrcode.Encode(util.BuildClientConfig(client, server, globalSettings), qrcode.Medium, 256)
212220
if err == nil {

store/store.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type IStore interface {
1010
GetGlobalSettings() (model.GlobalSetting, error)
1111
GetServer() (model.Server, error)
1212
GetClients(hasQRCode bool) ([]model.ClientData, error)
13-
GetClientByID(clientID string, hasQRCode bool) (model.ClientData, error)
13+
GetClientByID(clientID string, qrCode model.QRCodeSettings) (model.ClientData, error)
1414
SaveClient(client model.Client) error
1515
DeleteClient(clientID string) error
1616
SaveServerInterface(serverInterface model.ServerInterface) error

templates/clients.html

+47-21
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,18 @@ <h4 class="modal-title">QR Code</h4>
6868
<span aria-hidden="true">&times;</span>
6969
</button>
7070
</div>
71-
<img id="qr_code" class="w-100" style="image-rendering: pixelated;" src="" alt="QR code" />
71+
<div class="modal-body">
72+
<input type="hidden" id="qr_client_id" name="qr_client_id">
73+
<img id="qr_code" class="w-100" style="image-rendering: pixelated;" src="" alt="QR code" />
74+
<div class="form-group">
75+
<div class="icheck-primary d-inline">
76+
<input type="checkbox" id="qr_include_fwmark" onchange="regenerateQRCode()">
77+
<label for="qr_include_fwmark">
78+
Include FwMark
79+
</label>
80+
</div>
81+
</div>
82+
</div>
7283
</div>
7384
<!-- /.modal-content -->
7485
</div>
@@ -376,6 +387,37 @@ <h4 class="modal-title">Remove</h4>
376387
});
377388
});
378389

390+
// regenerateQRCode function for regenerating QR Code adding/removing some parts of configuration because of compatibility issues with some clients
391+
function regenerateQRCode() {
392+
const client_id = $("#qr_client_id").val();
393+
const QRCodeImg = $("#qr_code");
394+
let include_fwmark = false;
395+
if ($("#qr_include_fwmark").is(':checked')){
396+
include_fwmark = true;
397+
}
398+
QRCodeImg.hide();
399+
$.ajax({
400+
cache: false,
401+
method: 'GET',
402+
url: '{{.basePath}}/api/client/' + client_id,
403+
data: {
404+
qrCodeIncludeFwMark: include_fwmark
405+
},
406+
dataType: 'json',
407+
contentType: "application/json",
408+
success: function (resp) {
409+
const client = resp.Client;
410+
411+
$(".modal-title").text("Scan QR Code for " + client.name + " profile");
412+
QRCodeImg.attr('src', resp.QRCode).show();
413+
},
414+
error: function (jqXHR, exception) {
415+
const responseJson = jQuery.parseJSON(jqXHR.responseText);
416+
toastr.error(responseJson['message']);
417+
}
418+
});
419+
}
420+
379421
// submitEmailClient function for sending an email to the client with the configuration
380422
function submitEmailClient() {
381423
const client_id = $("#e_client_id").val();
@@ -453,7 +495,7 @@ <h4 class="modal-title">Remove</h4>
453495
const formId = $(form).attr('id');
454496
if (formId === "frm_edit_client") {
455497
submitEditClient();
456-
}else if (formId === "frm_email_client") {
498+
} else if (formId === "frm_email_client") {
457499
submitEmailClient();
458500
}
459501
}
@@ -486,31 +528,14 @@ <h4 class="modal-title">Remove</h4>
486528
let modal = $(this);
487529
const button = $(event.relatedTarget);
488530
const client_id = button.data('clientid');
489-
const QRCodeImg = modal.find("#qr_code");
490-
QRCodeImg.hide();
491-
$.ajax({
492-
cache: false,
493-
method: 'GET',
494-
url: '{{.basePath}}/api/client/' + client_id,
495-
dataType: 'json',
496-
contentType: "application/json",
497-
success: function (resp) {
498-
const client = resp.Client;
499531

500-
modal.find(".modal-title").text("Scan QR Code for " + client.name + " profile");
501-
QRCodeImg.attr('src', resp.QRCode).show();
502-
},
503-
error: function (jqXHR, exception) {
504-
const responseJson = jQuery.parseJSON(jqXHR.responseText);
505-
toastr.error(responseJson['message']);
506-
}
507-
});
532+
modal.find("#qr_client_id").val(client_id);
533+
regenerateQRCode();
508534
});
509535

510536
$(document).ready(function () {
511537
$.validator.setDefaults({
512538
submitHandler: function (form) {
513-
//submitEditClient();
514539
submitHandler(form);
515540
}
516541
});
@@ -563,6 +588,7 @@ <h4 class="modal-title">Remove</h4>
563588
$(element).removeClass('is-invalid');
564589
}
565590
});
591+
//
566592
});
567593
</script>
568594
{{end}}

util/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
6161
}
6262

6363
forwardMark := ""
64-
if setting.ForwardMark != "" && setting.ForwardMark != DefaultForwardMark {
64+
if setting.ForwardMark != "" {
6565
forwardMark = fmt.Sprintf("FwMark = %s\n", setting.ForwardMark)
6666
}
6767

0 commit comments

Comments
 (0)