wailsapp / wails

Create beautiful applications using Go
https://wails.io
MIT License
25.31k stars 1.22k forks source link

[linux] Weird behaviour with HTTPS with wails build command #2523

Open marcio199226 opened 1 year ago

marcio199226 commented 1 year ago

Description

Hi I have a backend here: https://server.mchat.ovh/ with TLS enabled and the certificates generated by lets encrypt certbot. When I running my app with wails dev everything goes ok, when running builded app I encounter this error message in webview console "Failed to load resource: WebKit encountered an internal error"

I think that the problem is caused by Origin header that in case of wails dev it's set to localhost:34115 and in case of wails build it's set to wails://wails and this custom protocol cause some troubles.

I can't figure out how can I fix it. I even tried some local http proxy like this:

package main

import (
    "embed"
    "log"
    "net/http"
    "net/http/httputil"
    "net/url"

    "github.com/wailsapp/wails/v2"
    "github.com/wailsapp/wails/v2/pkg/logger"
    "github.com/wailsapp/wails/v2/pkg/options"
    "github.com/wailsapp/wails/v2/pkg/options/linux"
    "github.com/wailsapp/wails/v2/pkg/options/mac"
    "github.com/wailsapp/wails/v2/pkg/options/windows"
)

//go:embed frontend/dist
var assets embed.FS

func handler(w http.ResponseWriter, r *http.Request) {
    localProxyUrl, _ := url.Parse("https://server.mchat.ovh/")
    w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
    w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")

    proxy := httputil.NewSingleHostReverseProxy(localProxyUrl)

    r.URL.Host = localProxyUrl.Host
    r.URL.Scheme = localProxyUrl.Scheme
    r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
    r.Host = localProxyUrl.Host

    proxy.ServeHTTP(w, r)
}

func main() {
    // Create an instance of the app structure
    app := NewApp()

    go func() {
        http.HandleFunc("/", handler)

        log.Println("Serving on localhost:8080")
        log.Fatal(http.ListenAndServe(":8080", nil))
    }()

    // Create application with options
    err := wails.Run(&options.App{
        Title:             "mchat-desktop",
        Width:             1024,
        Height:            768,
        MinWidth:          1024,
        MinHeight:         768,
        DisableResize:     false,
        Fullscreen:        false,
        Frameless:         true,
        StartHidden:       false,
        HideWindowOnClose: false,
        Assets:            assets,
        LogLevel:          logger.DEBUG,
        OnStartup:         app.startup,
        OnDomReady:        app.domReady,
        OnShutdown:        app.shutdown,
        Bind: []interface{}{
            app,
        },
        // Windows platform specific options
        Windows: &windows.Options{
            WebviewIsTransparent: false,
            WindowIsTranslucent:  false,
            DisableWindowIcon:    false,
        },
        Mac: &mac.Options{TitleBar: mac.TitleBarHidden()},
    })

    if err != nil {
        log.Fatal(err)
    }
}

But I still encounter the same error. I thought that the problem is only with self signed tls certificates but it's not working even with certs coming from lets encrypt. You can verify that the certs is working properly on https://server.mchat.ovh/ because the browser do not ask if you would to proceed to visit site with self signed cert. It's even work with curl or postman with ssl verification.

To Reproduce

I don't know how to provide steps to reproduce. I can give you an access to my repo or provide you a builded version if needed

Expected behaviour

I should can call external api without any troubles Tried on MacOs 12.6.x with M1 and it works for both wails build and wails dev calling directly https://server.mchat.ovh and http://localhost:8080 so it's definitely a problem on unix

p.s I have tried to build for windows/amd64 platform and run it through wine and unexpectedly it works there are no errors related to TLS

Screenshots

Schermata da 2023-03-23 12-08-11 Schermata da 2023-03-23 12-07-59 Schermata da 2023-03-23 12-06-58

Attempted Fixes

No response

System Details

wails doctor
Wails CLI v2.4.0

Scanning system - Please wait (this may take a long time)...Done.

# System

OS           | Ubuntu  
Version      | 18.04   
ID           | ubuntu  
Go Version   | go1.20.2
Platform     | linux   
Architecture | amd64   

# Wails

Version         | v2.4.0
Package Manager | apt   

# Dependencies

Dependency | Package Name          | Status    | Version                
*docker    | docker.io             | Installed | 23.0.1                 
gcc        | build-essential       | Installed | 12.4ubuntu1            
libgtk-3   | libgtk-3-dev          | Installed | 3.22.30-1ubuntu4       
libwebkit  | libwebkit2gtk-4.0-dev | Installed | 2.32.4-0ubuntu0.18.04.1
npm        | npm                   | Installed | 6.14.17                
*nsis      | nsis                  | Installed | v2.51-1                
pkg-config | pkg-config            | Installed | 0.29.1-0ubuntu2        
* - Optional Dependency

# Diagnosis

Your system is ready for Wails development!
 ♥   If Wails is useful to you or your company, please consider sponsoring the project:
https://github.com/sponsors/leaanthony


### Additional context

_No response_
leaanthony commented 1 year ago

What JS code are you using to call the API? Is there a reason you want to make these API calls from JS rather than Go? I'd call a bound Go method to go get the data rather than rely on webview implementations as it'll be far more flexible and likely to work everywhere.

marcio199226 commented 1 year ago

It's simply a http lib from angular

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';

@Injectable({ providedIn: 'root' })
export class AuthApi {
  constructor(private http: HttpClient) { }

  signin(payload: any): Observable<any> {
    return this.http.post(`${environment.ApiBaseUrl}/auth/login`, payload);
  }

  singup(payload: any): Observable<any> {
    return this.http.post(`${environment.ApiBaseUrl}/registration`, payload);
  }
}

Is there a reason you want to make these API calls from JS rather than Go Yeah I want to treat the fe part as normal SPA that makes api related stuff is more straightforward in ts than in golang. I want to use the be part only for rsa management, encrypt/decrypt of messages, do gpg file encryption and so on...

Anyway on MacOs and Windows it works calling straight my server or even passing through golang reverse proxy, it even work launching *.exe from wine.

If you trust me or you have some vm with some newer debian distro that mine maybe you can try if it works, simply open the app e try to login with any credentials you want then check by inspector if the api responds correctly or there is an error. mchat-desktop.zip

Maybe its somehow related to ubuntu 18.04...?!?

P.S

I'd call a bound Go method to go get the data rather than rely on webview implementations as it'll be far more flexible and likely to work everywhere.

Please note that it even doesn't work with golang proxy, so I'm quite sure that's related to ubuntu 18.04 or some "old" dependecies (webkitgtk related stuff ?) or the webview itself on ubuntu ? What's the repository of webview implementations for unix like systems ? maybe I can search if there are any issues related to TLS errors

leaanthony commented 1 year ago

@marcio199226 Please can you retry using the latest version. Remember to upgrade the version of Wails in you project's go.mod.

marcio199226 commented 1 year ago

Unfortunately I have the same issue on v2.5.0 and v2.5.1 :(

Wails CLI v2.5.1

 SUCCESS  Done.                                                                                                                                 

# System

OS           | Ubuntu  
Version      | 18.04   
ID           | ubuntu  
Go Version   | go1.20.2
Platform     | linux   
Architecture | amd64   

# Wails

Version         | v2.5.1
Package Manager | apt   

# Dependencies

Dependency | Package Name          | Status    | Version                
*docker    | docker.io             | Installed | 23.0.6                 
gcc        | build-essential       | Installed | 12.4ubuntu1            
libgtk-3   | libgtk-3-dev          | Installed | 3.22.30-1ubuntu4       
libwebkit  | libwebkit2gtk-4.0-dev | Installed | 2.32.4-0ubuntu0.18.04.1
npm        | npm                   | Installed | 6.14.17                
*nsis      | nsis                  | Installed | v2.51-1                
pkg-config | pkg-config            | Installed | 0.29.1-0ubuntu2        
* - Optional Dependency

# Diagnosis

Your system is ready for Wails development!
 ♥   If Wails is useful to you or your company, please consider sponsoring the project:
https://github.com/sponsors/leaanthony