gpt4 book ai didi

php - wss 在 apache 服务器上故障转移 https

转载 作者:搜寻专家 更新时间:2023-10-31 21:06:57 25 4
gpt4 key购买 nike

我在 digital ocean 中有一台服务器,它使用 StartCOM I 类初级中间 CA 进行 ssl。我已经设置了一个 websocket 服务器,我想从一个由 https 提供服务的页面连接到它。

当我尝试仅使用 http 连接到 websocket 时,它工作正常。但是当我尝试通过将 websocket uri 从 ws 更改为 wss 来通过 https 使用它时,它不会连接。

我做错了什么。使用 fancywebsockets.js 建立连接

fancywebsockets.js

var FancyWebSocket = function(url)
{
var callbacks = {};
var ws_url = url;
var conn;

this.bind = function(event_name, callback){
callbacks[event_name] = callbacks[event_name] || [];
callbacks[event_name].push(callback);
return this;// chainable
};

this.send = function(event_name, event_data){
this.conn.send( event_data );
return this;
};

this.connect = function() {
if ( typeof(MozWebSocket) == 'function' )
this.conn = new MozWebSocket(url);
else
this.conn = new WebSocket(url);

// dispatch to the right handlers
this.conn.onmessage = function(evt){
dispatch('message', evt.data);
};

this.conn.onclose = function(){dispatch('close',null)}
this.conn.onopen = function(){dispatch('open',null)}
};

this.disconnect = function() {
this.conn.close();
};

var dispatch = function(event_name, message){
var chain = callbacks[event_name];
if(typeof chain == 'undefined') return; // no callbacks for this event
for(var i = 0; i < chain.length; i++){
chain[i]( message )
}
}
};

客户端 JS

var Server;

function log( text ) {
console.log(text)
}

function send( text ) {
Server.send( 'message', text );
}

$(document).ready(function() {
log('Connecting...');
Server = new FancyWebSocket('ws://www.myserver.com:9400');

$('#message').keypress(function(e) {
if ( e.keyCode == 13 && this.value ) {
log( 'You: ' + this.value );
send( this.value );


}
});

//Let the user know we're connected
Server.bind('open', function() {
log( "Connected." );
});

//OH NOES! Disconnection occurred.
Server.bind('close', function( data ) {
log( "Disconnected." );
});

//Log any messages sent from server
Server.bind('message', function( payload ) {
log( payload );
});

Server.connect();
});

Server.php

<?php
// prevent the server from timing out
set_time_limit(0);

// include the web sockets server script (the server is started at the far bottom of this file)
require 'class.PHPWebSocket.php';

// when a client sends data to the server
function wsOnMessage($clientID, $message, $messageLength, $binary) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );

// check if message length is 0
if ($messageLength == 0) {
$Server->wsClose($clientID);
return;
}

//The speaker is the only person in the room. Don't let them feel lonely.
if ( sizeof($Server->wsClients) == 1 )
$Server->wsSend($clientID, "There isn't anyone else in the room, but I'll still listen to you. --Your Trusty Server");
else
//Send the message to everyone but the person who said it
foreach ( $Server->wsClients as $id => $client )
if ( $id != $clientID )
$Server->wsSend($id, "Visitor $clientID ($ip) said \"$message\"");
}

// when a client connects
function wsOnOpen($clientID)
{
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );

$Server->log( "$ip ($clientID) has connected." );

//Send a join notice to everyone but the person who joined
foreach ( $Server->wsClients as $id => $client )
if ( $id != $clientID )
$Server->wsSend($id, "Visitor $clientID ($ip) has joined the room.");
}

// when a client closes or lost connection
function wsOnClose($clientID, $status) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );

$Server->log( "$ip ($clientID) has disconnected." );

//Send a user left notice to everyone in the room
foreach ( $Server->wsClients as $id => $client )
$Server->wsSend($id, "Visitor $clientID ($ip) has left the room.");
}

// start the server
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
// for other computers to connect, you will probably need to change this to your LAN IP or external IP,
// alternatively use: gethostbyaddr(gethostbyname($_SERVER['SERVER_NAME']))
$Server->wsStartServer('my.ip.add.ress', 9400);

?>

服务器截图

enter image description here

总是掉线

最佳答案

我遇到了同样的问题(我什至使用相同的 PHP 脚本)并设法使用 stunnel 解决了这个问题。通过各种不同的、相互矛盾的来源的大量反复试验,我设法让它运行起来。但是,您不会再获得客户端 IP 地址,如果需要,您必须手动将其发送到服务器。我在某处读到您可以使用代理转发客户端 IP,但我自己还无法设置。

在服务器上安装 stunnel

通过 ssh 登录到您的服务器,确保您的系统是完全最新的。

apt-get update
apt-get upgrade

安装stunnel包

apt-get install stunnel4 -y

配置 channel

通过运行以下命令为 stunnel 创建一个配置文件

nano /etc/stunnel/stunnel.conf

在文件中写入以下内容

foreground = yes
key = /path/to/your/ssl/cert.key
cert = /path/to/your/ssl/cert.pem
CAfile = /path/to/your/ssl/cert.pem
debug = 7
output = /var/log/stunnel_websocket.log

[websocket]
accept = myserver.com:9401
connect = 9400

就我个人而言,我只是使用了与 HTTPS 设置相同的证书作为 stunnel。

运行 stunnel

您必须保持 stunnel 运行,就像您必须保持 websocket 运行一样。使用以下命令执行此操作:

/etc/init.d/stunnel4 restart

改变你的 PHP 脚本

更改 PHP 文件 server.php

$Server->wsStartServer('my.ip.add.ress', 9400);

$Server->wsStartServer('localhost', 9400);

修改客户端JS脚本

Server = new FancyWebSocket('ws://www.myserver.com:9400');

应该改为

Server = new FancyWebSocket('wss://www.myserver.com:9401');

因此,如果您正在运行 stunnel 和 websocket,您现在应该能够再次通过套接字发送和接收数据,唯一的区别是根据 websocket 脚本,所有客户端现在似乎都来自 127.0.0.1。

关于php - wss 在 apache 服务器上故障转移 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30872839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com