warmcat / libwebsockets

canonical libwebsockets.org networking library
https://libwebsockets.org
Other
4.74k stars 1.48k forks source link

Problem reproducing generic-session examples #661

Closed d30jeff closed 7 years ago

d30jeff commented 7 years ago

I've been following this tutorial.

I have a test directory located at /var/www/testing/ that contain these files:

├── generic-sessions
│   ├── assets
│   │   ├── admin-login.html
│   │   ├── failed-login.html
│   │   ├── index.html
│   │   ├── lws-common.js
│   │   ├── lwsgs.js
│   │   ├── lwsgs-logo.png
│   │   ├── md5.min.js
│   │   ├── post-forgot-fail.html
│   │   ├── post-forgot-ok.html
│   │   ├── post-register-fail.html
│   │   ├── post-register-ok.html
│   │   ├── post-verify-fail.html
│   │   ├── post-verify-ok.html
│   │   ├── seats.jpg
│   │   ├── sent-forgot-fail.html
│   │   ├── sent-forgot-ok.html
│   │   └── successful-login.html
│   ├── failed-login.html
│   ├── handlers.c
│   ├── index.html
│   ├── lwsgs.js
│   ├── lwsgs-logo.png
│   ├── needadmin
│   │   └── admin-login.html
│   ├── needauth
│   │   └── successful-login.html
│   ├── post-forgot-fail.html
│   ├── post-forgot-ok.html
│   ├── post-register-fail.html
│   ├── post-register-ok.html
│   ├── post-verify-fail.html
│   ├── post-verify-ok.html
│   ├── private-lwsgs.h
│   ├── protocol_generic_sessions.c
│   ├── protocol_lws_messageboard.c
│   ├── seats.jpg
│   ├── sent-forgot-fail.html
│   ├── sent-forgot-ok.html
│   └── utils.c
├── index.html
└── lwsgs.js

My configuration file is stored at /etc/lwsws/conf.d/generic

{
  "vhosts": [{
    "name": "eth0",
    "port": "8080",
    "interface": "lo",
    "mounts": [{
      # things in here can always be served
      "mountpoint": "/",
      "origin": "file:///var/www/testing",
      "default": "index.html"
    }],
    "ws-protocols": [{
      "stats-streamer": {
        "status": "ok"
      },
      "protocol-generic-sessions": {
        "status": "ok",
        "admin-user": "root",
        "admin-password-sha1": "a75acff76313682f83991f571778aadbec2828de",
        "session-db": "/var/www/sessions/lws.sqlite3",
        "timeout-idle-secs": "600",
        "timeout-anon-idle-secs": "1200",
        "timeout-absolute-secs": "6000",
        "confounder": "ni12i3mi912moqwmi9eqwemqwie9wqeok12"
      }
    }]
  }]
}

When I head over to localhost:8080 I get this error saying that lwsws is misconfigured.

Image

Thanks

lws-team commented 7 years ago

The error is telling you correctly, because it shouldn't show $lwsgs_user in the main window. This should get rewritten.

At the mountpoint config where you place this, you need some "interpret" args like this

 {
        # things in here can always be served
        "mountpoint": "/lwsgs",
        "origin": "file://_lws_ddir_/libwebsockets-test-server/generic-sessions",
        "origin": "callback://protocol-lws-messageboard",
        "default": "index.html",
        "auth-mask": "0",
        "interpret": {
                ".js": "protocol-lws-messageboard"
        }
       }, {
        # things in here can only be served if logged in as a user
        "mountpoint": "/lwsgs/needauth",
        "origin": "file://_lws_ddir_/libwebsockets-test-server/generic-sessions/needauth",
        "origin": "callback://protocol-lws-messageboard",
        "auth-mask": "1", # b0 = logged in with any user name
        "interpret": {
                ".js": "protocol-lws-messageboard"
        }
       }, {
        # things in here can only be served if logged in as admin
        "mountpoint": "/lwsgs/needadmin",
        "origin": "file://_lws_ddir_/libwebsockets-test-server/generic-sessions/needadmin",
        "origin": "callback://protocol-lws-messageboard",
        "auth-mask": "3", # b1 = admin, b0 = logged in with any user name
        "interpret": {
                ".js": "protocol-lws-messageboard"

        }

They cause the content in files matching the suffix to be passed through the given protocol's LWS_CALLBACK_PROCESS_HTML before being served. The messageboard protocol passes it back to the generic-sessions implementation, which replaces these strings

                "$lwsgs_user",
                "$lwsgs_auth",
                "$lwsgs_email"

with the active values from the current session, before passing the buffer back up to be served.

d30jeff commented 7 years ago

Thanks so much for pointing me to the right direction. I have managed to replicate the demo with the help of the config that you've given. image

Thanks a lot.