Skip to content

Commit

Permalink
2.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
bitepeng committed Dec 13, 2023
1 parent 22477a2 commit 121ddfa
Show file tree
Hide file tree
Showing 16 changed files with 2,629 additions and 125 deletions.
133 changes: 133 additions & 0 deletions apps/pass/lib/chat/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package chat

import (
"bytes"
"log"
"time"

"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)

const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second

// Time allowed to read the next pong message from the peer.
pongWait = 60 * time.Second

// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10

// Maximum message size allowed from peer.
maxMessageSize = 2048000
)

var (
newline = []byte{'\n'}
space = []byte{' '}
)

var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}

// Client is a middleman between the websocket connection and the hub.
type Client struct {
hub *Hub

// The websocket connection.
conn *websocket.Conn

// Buffered channel of outbound messages.
send chan []byte
}

// readPump pumps messages from the websocket connection to the hub.
//
// The application runs readPump in a per-connection goroutine. The application
// ensures that there is at most one reader on a connection by executing all
// reads from this goroutine.
func (c *Client) readPump() {
defer func() {
c.hub.unregister <- c
c.conn.Close()
}()
c.conn.SetReadLimit(maxMessageSize)
c.conn.SetReadDeadline(time.Now().Add(pongWait))
c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, message, err := c.conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("error: %v", err)
}
break
}
message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
c.hub.broadcast <- message
}
}

// writePump pumps messages from the hub to the websocket connection.
//
// A goroutine running writePump is started for each connection. The
// application ensures that there is at most one writer to a connection by
// executing all writes from this goroutine.
func (c *Client) writePump() {
ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
c.conn.Close()
}()
for {
select {
case message, ok := <-c.send:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if !ok {
// The hub closed the channel.
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}

w, err := c.conn.NextWriter(websocket.TextMessage)
if err != nil {
return
}
w.Write(message)

// Add queued chat messages to the current websocket message.
n := len(c.send)
for i := 0; i < n; i++ {
w.Write(newline)
w.Write(<-c.send)
}

if err := w.Close(); err != nil {
return
}
case <-ticker.C:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
return
}
}
}
}

// serveWs handles websocket requests from the peer.
func ServeWs(hub *Hub, c *gin.Context) {
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
return
}
//defer conn.Close()
client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)}
client.hub.register <- client

// Allow collection of memory referenced by the caller by doing all work in
// new goroutines.
go client.writePump()
go client.readPump()
}
49 changes: 49 additions & 0 deletions apps/pass/lib/chat/hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package chat

// Hub maintains the set of active clients and broadcasts messages to the
// clients.
type Hub struct {
// Registered clients.
clients map[*Client]bool

// Inbound messages from the clients.
broadcast chan []byte

// Register requests from the clients.
register chan *Client

// Unregister requests from clients.
unregister chan *Client
}

func NewHub() *Hub {
return &Hub{
broadcast: make(chan []byte),
register: make(chan *Client),
unregister: make(chan *Client),
clients: make(map[*Client]bool),
}
}

func (h *Hub) Run() {
for {
select {
case client := <-h.register:
h.clients[client] = true
case client := <-h.unregister:
if _, ok := h.clients[client]; ok {
delete(h.clients, client)
close(client.send)
}
case message := <-h.broadcast:
for client := range h.clients {
select {
case client.send <- message:
default:
close(client.send)
delete(h.clients, client)
}
}
}
}
}
6 changes: 3 additions & 3 deletions apps/pass/lib/keys/key_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
package keys

import (
//"github.com/go-vgo/robotgo"
"github.com/go-vgo/robotgo"
)

// SendKey 主电脑键盘
func SendKey(k string) {
/*

switch k {
case "esc":
robotgo.KeyTap(`esc`)
Expand Down Expand Up @@ -50,5 +50,5 @@ func SendKey(k string) {
default:
robotgo.KeyTap(k)
}
*/

}
10 changes: 10 additions & 0 deletions apps/pass/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"b0go/apps/pass/lib/chat"
"b0go/core/engine"
"embed"
"io/fs"
Expand Down Expand Up @@ -48,6 +49,7 @@ func run() {
engine.Gin.Use(engine.CorsMiddleware())
routeStatic(config.Live)
routeApi()
routeWs()
}

// 注册静态路由
Expand Down Expand Up @@ -109,3 +111,11 @@ func GETX(url, param, title string, handle gin.HandlerFunc) {
func POSTX(url, param, title string, handle gin.HandlerFunc) {
engine.Router(appId, "POST", url, param, "(Auth)"+title, engine.JWTMiddleware(), handle)
}

func routeWs() {
hub := chat.NewHub()
go hub.Run()
engine.Gin.GET("/ws", func(c *gin.Context) {
chat.ServeWs(hub, c)
})
}
1 change: 1 addition & 0 deletions apps/pass/ui/dist/assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ body{
}
.logo{
margin-right:50px;font-weight: bold;
text-align: left;
}
.pull-right{
float: right;
Expand Down
21 changes: 0 additions & 21 deletions apps/pass/ui/dist/assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,26 +409,6 @@ layui.use(['tree', 'table','form','dropdown','util'], function(){
openPage("/files"+currPath,{});
})

//键盘
$("#btn_left_key").on("click",function(){
layer.open({
title: "主电脑键盘遥控",
area: areaBig,
type: 1,
content: '<div class="padding15"><div class="layui-form-item" align="center"><p><br></p></div>'+$("#send-key").html()+'</div>',
cancel: function () {}
});
})
window.sendKey=function(k){
$.ajax({
url: "/pass/cmd-key?k="+k,
method: "get",
data: {},
success: function(res) {
layer.msg("按下"+k+"");
}
});
}

//打开按钮
$("#btn_left_dir").on("click",function(){
Expand Down Expand Up @@ -478,7 +458,6 @@ layui.use(['tree', 'table','form','dropdown','util'], function(){
console.log("::Config::",res.data);
//linux操作系统禁用一些功能
if(res.data.Password!="windows"){
domid("btn_left_key").style.display="none";
domid("btn_left_dir").style.display="none";
}
ips=(res.data.ListenAddr).split(":");
Expand Down
98 changes: 98 additions & 0 deletions apps/pass/ui/dist/assets/js/qrcode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
layui.use(['layer'], function(){
var $ = layui.jquery
,layer = layui.layer;

function getIP(){
var servIP;
$.ajax({
url: "/gateway/config",
success: function(res) {
console.log("::Config::",res.data);
//linux操作系统禁用一些功能
if(res.data.Password!="windows"){
domid("btn_left_key").style.display="none";
}
ips=(res.data.ListenAddr).split(":");
servPort=":"+(res.data.ListenAddr).split(":")[1];
if(ips[0]!=""){
if(ips[0]="127.0.0.1"){
alert("如果IP被设置为127.0.0.1,意味着只能本机使用,将无法分享文件!\n若无特殊需求,请将配置文件的'ListenAddr'设置为纯端口,如':8899'。");
}
servIP=ips[0];
setTextValue("http://"+servIP+servPort);
console.log("::ServIP::",servIP);
}
//兼容域名情况
if(res.data.Domain!=""){
servIP=res.data.Domain;servPort="";
setTextValue("http://"+servIP+servPort);
console.log("::ServIP::",servIP);
}else{
$.ajax({
url: "/pass/read-ip",
success: function(res) {
servIP=res.data;
setTextValue("http://"+servIP+servPort);
console.log("::ServIP::",servIP);
}
});
}
}
});
}

window.onload=function () {
document.getElementById('text').style.display="";
document.getElementById('selects').style.display="none";
var ip=args('f');
if(ip){
document.getElementById('text').value="http://"+ip;
makeCode();
}else{
getIP();
}
};

function args(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURI(r[2]); return null;
}

//键盘
$("#btn_left_key").on("click",function(){
layer.open({
title: "遥控主电脑键盘",
area: ['100%','100%'],
type: 1,
content: '<div class="padding15"><div class="layui-form-item" align="center"><p><br></p></div>'+$("#send-key").html()+'</div>',
cancel: function () {}
});
})
window.sendKey=function(k){
$.ajax({
url: "/pass/cmd-key?k="+k,
method: "get",
data: {},
success: function(res) {
layer.msg("按下"+k+"键");
}
});
}

});
function setTextValue(v){
document.getElementById('text').value=v;
makeCode();
}
function makeCode(){
document.getElementById("qrcode").innerHTML="";
new QRCode(document.getElementById("qrcode"), {
text: document.getElementById('text').value,
width: 235,
height: 235,
colorDark : "#226ef1",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H
});
}
Loading

0 comments on commit 121ddfa

Please sign in to comment.