Skip to content

Commit

Permalink
Added checkbox FwMark in QRCode generation (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsvit-vlasov-y authored Dec 2, 2022
1 parent 9d2dd71 commit a80741e
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 30 deletions.
24 changes: 19 additions & 5 deletions handler/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,15 @@ func GetClient(db store.IStore) echo.HandlerFunc {
return func(c echo.Context) error {

clientID := c.Param("id")
clientData, err := db.GetClientByID(clientID, true)
qrCodeIncludeFwMark := c.QueryParam("qrCodeIncludeFwMark")
qrCodeSettings := model.QRCodeSettings{
Enabled: true,
IncludeDNS: true,
IncludeFwMark: qrCodeIncludeFwMark == "true",
IncludeMTU: true,
}

clientData, err := db.GetClientByID(clientID, qrCodeSettings)
if err != nil {
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Client not found"})
}
Expand Down Expand Up @@ -257,7 +265,13 @@ func EmailClient(db store.IStore, mailer emailer.Emailer, emailSubject, emailCon
c.Bind(&payload)
// TODO validate email

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

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

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

clientData, err := db.GetClientByID(clientID, false)
clientData, err := db.GetClientByID(clientID, model.QRCodeSettings{Enabled: false})
if err != nil {
log.Errorf("Cannot generate client id %s config file for downloading: %v", clientID, err)
return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Client not found"})
Expand Down
7 changes: 7 additions & 0 deletions model/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ type ClientData struct {
Client *Client
QRCode string
}

type QRCodeSettings struct {
Enabled bool
IncludeDNS bool
IncludeFwMark bool
IncludeMTU bool
}
12 changes: 10 additions & 2 deletions store/jsondb/jsondb.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (o *JsonDB) GetClients(hasQRCode bool) ([]model.ClientData, error) {
return clients, nil
}

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

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

// generate client qrcode image in base64
if hasQRCode && client.PrivateKey != "" {
if qrCodeSettings.Enabled && client.PrivateKey != "" {
server, _ := o.GetServer()
globalSettings, _ := o.GetGlobalSettings()
client := client
client.UseServerDNS = qrCodeSettings.IncludeDNS
if !qrCodeSettings.IncludeMTU {
globalSettings.MTU = 0
}
if !qrCodeSettings.IncludeFwMark {
globalSettings.ForwardMark = ""
}

png, err := qrcode.Encode(util.BuildClientConfig(client, server, globalSettings), qrcode.Medium, 256)
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type IStore interface {
GetGlobalSettings() (model.GlobalSetting, error)
GetServer() (model.Server, error)
GetClients(hasQRCode bool) ([]model.ClientData, error)
GetClientByID(clientID string, hasQRCode bool) (model.ClientData, error)
GetClientByID(clientID string, qrCode model.QRCodeSettings) (model.ClientData, error)
SaveClient(client model.Client) error
DeleteClient(clientID string) error
SaveServerInterface(serverInterface model.ServerInterface) error
Expand Down
68 changes: 47 additions & 21 deletions templates/clients.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,18 @@ <h4 class="modal-title">QR Code</h4>
<span aria-hidden="true">&times;</span>
</button>
</div>
<img id="qr_code" class="w-100" style="image-rendering: pixelated;" src="" alt="QR code" />
<div class="modal-body">
<input type="hidden" id="qr_client_id" name="qr_client_id">
<img id="qr_code" class="w-100" style="image-rendering: pixelated;" src="" alt="QR code" />
<div class="form-group">
<div class="icheck-primary d-inline">
<input type="checkbox" id="qr_include_fwmark" onchange="regenerateQRCode()">
<label for="qr_include_fwmark">
Include FwMark
</label>
</div>
</div>
</div>
</div>
<!-- /.modal-content -->
</div>
Expand Down Expand Up @@ -376,6 +387,37 @@ <h4 class="modal-title">Remove</h4>
});
});

// regenerateQRCode function for regenerating QR Code adding/removing some parts of configuration because of compatibility issues with some clients
function regenerateQRCode() {
const client_id = $("#qr_client_id").val();
const QRCodeImg = $("#qr_code");
let include_fwmark = false;
if ($("#qr_include_fwmark").is(':checked')){
include_fwmark = true;
}
QRCodeImg.hide();
$.ajax({
cache: false,
method: 'GET',
url: '{{.basePath}}/api/client/' + client_id,
data: {
qrCodeIncludeFwMark: include_fwmark
},
dataType: 'json',
contentType: "application/json",
success: function (resp) {
const client = resp.Client;

$(".modal-title").text("Scan QR Code for " + client.name + " profile");
QRCodeImg.attr('src', resp.QRCode).show();
},
error: function (jqXHR, exception) {
const responseJson = jQuery.parseJSON(jqXHR.responseText);
toastr.error(responseJson['message']);
}
});
}

// submitEmailClient function for sending an email to the client with the configuration
function submitEmailClient() {
const client_id = $("#e_client_id").val();
Expand Down Expand Up @@ -453,7 +495,7 @@ <h4 class="modal-title">Remove</h4>
const formId = $(form).attr('id');
if (formId === "frm_edit_client") {
submitEditClient();
}else if (formId === "frm_email_client") {
} else if (formId === "frm_email_client") {
submitEmailClient();
}
}
Expand Down Expand Up @@ -486,31 +528,14 @@ <h4 class="modal-title">Remove</h4>
let modal = $(this);
const button = $(event.relatedTarget);
const client_id = button.data('clientid');
const QRCodeImg = modal.find("#qr_code");
QRCodeImg.hide();
$.ajax({
cache: false,
method: 'GET',
url: '{{.basePath}}/api/client/' + client_id,
dataType: 'json',
contentType: "application/json",
success: function (resp) {
const client = resp.Client;

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

$(document).ready(function () {
$.validator.setDefaults({
submitHandler: function (form) {
//submitEditClient();
submitHandler(form);
}
});
Expand Down Expand Up @@ -563,6 +588,7 @@ <h4 class="modal-title">Remove</h4>
$(element).removeClass('is-invalid');
}
});
//
});
</script>
{{end}}
2 changes: 1 addition & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
}

forwardMark := ""
if setting.ForwardMark != "" && setting.ForwardMark != DefaultForwardMark {
if setting.ForwardMark != "" {
forwardMark = fmt.Sprintf("FwMark = %s\n", setting.ForwardMark)
}

Expand Down

0 comments on commit a80741e

Please sign in to comment.