Skip to content

Commit

Permalink
tap/tun connect server with the websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
killinux committed Jun 24, 2020
1 parent a10e7b1 commit f8abdc7
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 77 deletions.
7 changes: 2 additions & 5 deletions CMOS.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/*
JSLinux-deobfuscated - An annotated version of the original JSLinux.
Original is Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's permission.
CMOS Ram Memory, actually just the RTC Clock Emulator
Useful references:
------------------
http://www.bioscentral.com/misc/cmosmap.htm
http://wiki.osdev.org/CMOS
add by hao:
cmos的核心模拟代码
*/

/*
Expand Down
6 changes: 1 addition & 5 deletions KBD.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/*
JSLinux-deobfuscated - An annotated version of the original JSLinux.
Original is Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's permission.
Keyboard Device Emulator
add by hao:键盘io的交互代码
*/
function KBD(PC, reset_callback) {
PC.register_ioport_read(0x64, 1, 1, this.read_status.bind(this));
Expand Down
11 changes: 4 additions & 7 deletions PCEmulator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/*
JSLinux-deobfuscated - An annotated version of the original JSLinux.
Original is Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's permission.
Main PC Emulator Routine
add by hao
模拟器的核心代码
*/

// used as callback wrappers for emulated PIT and PIC chips
Expand Down Expand Up @@ -266,6 +262,7 @@ pc_disk.prototype.exec_cmd = function(ga) {
//add by hao sector end

//add by hao ide0 begin
// ide硬盘相关的核心代码,部分混淆的代码没有修正
//////////////ide_driver
ide_driver.prototype.ioport_write = function(fa, ga) {
var s = this.cur_drive;
Expand Down Expand Up @@ -994,7 +991,7 @@ network_driver.prototype.reset_ioport_read = function(fa) {
this.reset();
};

function network_driver(this_pc, base, set_irq_function, arr, send_function) {//hao network
function network_driver(this_pc, base, set_irq_function, arr, send_function) {//hao network,网络驱动
var i;
this.set_irq_func = set_irq_function;
this.send_packet_func = send_function;
Expand Down
4 changes: 2 additions & 2 deletions PIC.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
JSLinux-deobfuscated - An annotated version of the original JSLinux.
JSLinux-tap - An annotated version of the original JSLinux.
Original is Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's permission.
add by hao:use the driver of 8259A
8259A PIC (Programmable Interrupt Controller) Emulation Code
The 8259 combines multiple interrupt input sources into a single
Expand Down
4 changes: 2 additions & 2 deletions PIT.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
JSLinux-deobfuscated - An annotated version of the original JSLinux.
JSLinux-tap - An annotated version of the original JSLinux.
Original is Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's permission.
add by hao : use the driver of 8254
8254 Programmble Interrupt Timer Emulator
Useful References
Expand Down
4 changes: 3 additions & 1 deletion Serial.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
JSLinux-deobfuscated - An annotated version of the original JSLinux.
JSLinux-tap - An annotated version of the original JSLinux.
Original is Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's permission.
Serial Controller Emulator
add by hao:
串口的驱动,设备/dev/clipboard 用串口驱动链接浏览器部分的代码,参考编译内核的jsclipboard部分
*/

function Serial(Ng, mem8_loc, kh, lh) {
Expand Down
8 changes: 2 additions & 6 deletions clipboard.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/*
JSLinux-deobfuscated - An annotated version of the original JSLinux.
Original is Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's permission.
Clipboard Device
add by hao :
clipboard.js 是剪切板交互io的核心代码,对应设备/dev/clipboard
*/
function clipboard_device(Ng, Zf, rh, lh, sh) {
Ng.register_ioport_read(Zf, 16, 4, this.ioport_readl.bind(this));
Expand Down
24 changes: 13 additions & 11 deletions cpux86-ta.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
JSLinux-deobfuscated - An annotated version of the original JSLinux.
Original is Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's permission.
add by hao :
1.修改了部分驱动和网络链接websocket的部分
2.修改了load_binary,加载linuxstart.bin和内核的加载函数,有先后顺序,后续可以重构成promise的版本
3.这部分代码最好结合《深入理解linux内核架构》一起学习
A x86 CPU (circa 486 sans FPU) Emulator
======================================================================
Expand All @@ -18,8 +18,9 @@ http://en.wikipedia.org/wiki/X86
http://en.wikipedia.org/wiki/Control_register
http://en.wikipedia.org/wiki/X86_assembly_language
http://en.wikipedia.org/wiki/Translation_lookaside_buffer
原版是这个
http://bellard.org/jslinux/tech.html :
但是这个目前已经是wasm的版本了,当前版本还是纯js的,提供linux内核学习使用
The exact restrictions of the emulated CPU are:
- No FPU/MMX/SSE
- No segment limit and right checks when accessing memory
Expand Down Expand Up @@ -513,7 +514,7 @@ CPU_X86.prototype.dump = function() {
str += " ";
}
}
descriptor_table = this.gdt;//hao
descriptor_table = this.gdt;//add by hao ,这里定义linux内核的GDT和IDT
str = "GDT= " + _4_bytes_(descriptor_table.base) + " " + _4_bytes_(descriptor_table.limit) + " ";
descriptor_table = this.idt;
str += "IDT= " + _4_bytes_(descriptor_table.base) + " " + _4_bytes_(descriptor_table.limit);
Expand Down Expand Up @@ -2930,7 +2931,7 @@ CPU_X86.prototype.exec_internal = function(N_cycles, interrupt) {
n++;
break;
default:
n += 2;//hao
n += 2;//add by hao,cs处理器的默认操作
break;
}
} else {
Expand Down Expand Up @@ -3992,7 +3993,8 @@ CPU_X86.prototype.exec_internal = function(N_cycles, interrupt) {
cpu.segs[1].base = (selector << 4);
cpu.eflags &= ~(0x00000200 | 0x00000100 | 0x00040000 | 0x00010000);
}
function do_interrupt(intno, ne, error_code, oe, pe) {//hao
function do_interrupt(intno, ne, error_code, oe, pe) {
//add by hao:中断的定义,这部分可以参考《深入理解linux内核架构》那本书一起学习
if (intno == 0x06) {
var eip_tmp = eip;
var eip_offset;
Expand Down Expand Up @@ -4823,8 +4825,8 @@ CPU_X86.prototype.exec_internal = function(N_cycles, interrupt) {
}
return 1;
}
//hao: Verify a Segment for Reading or Writing
//https://pdos.csail.mit.edu/6.828/2005/readings/i386/VERR.htm
//add by hao: Verify a Segment for Reading or Writing
//参考 https://pdos.csail.mit.edu/6.828/2005/readings/i386/VERR.htm
function op_VERR_VERW(selector, is_verw) {
var z;
z = segment_isnt_accessible(selector, is_verw);
Expand Down Expand Up @@ -9709,7 +9711,7 @@ CPU_X86.prototype.exec = function(N_cycles) {
==========================================================================================
This routine loads a binary array into memory.
*/
//hao
//delete by hao, 这里是老版本,不能支持大硬盘,修改了这里的代码,加入一个传递参数,其实这里修改成promise其实更优雅
/*CPU_X86.prototype.load_binary = function(binary_array, mem8_loc) {
var len, i, typed_binary_array;
len = binary_array.byteLength;
Expand Down
7 changes: 2 additions & 5 deletions cpux86-ta.original.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/*
PC Emulator
add by hao
Copyright (c) 2011 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's
permission.
这里备份一下原始版本
*/
"use strict";
var aa = [1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1];
Expand Down
4 changes: 2 additions & 2 deletions echo.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<title>this is a test for websocket</title>
</head>
<script type="text/javascript">
String.prototype.startWith=function(s){
Expand Down Expand Up @@ -89,7 +89,7 @@
</script>
<body onload="startServer()">
<input id="serverip" type="text" size="20"
value="ws://13.231.152.115:3000/echo" />
value="ws://www.hackernel.com:3000/echo" />

</br>
您的Id:<input id="username" type="text" readonly/></br>
Expand Down
40 changes: 21 additions & 19 deletions jslinux.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
/*
Linux launcher
Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's
permission.
*/
"use strict";

var term, pc, boot_start_time, init_state;

/*
add by hao 终端的代码
*/
function term_start()
{
term = new Term(80, 30, term_handler);
Expand All @@ -22,7 +16,7 @@ function term_handler(str)
{
try
{
pc.serial.send_chars(str);
pc.serial.send_chars(str);//add by hao,这里和/dev/clipboard交互
}
catch(e)
{
Expand All @@ -31,12 +25,12 @@ function term_handler(str)
}
var net_handler = function net_handler(str)
{
// FIXME: name this more appropriately.
// add by hao: 网络交互的核心内容,红黄交替代表网络可用,红色说明websocket已经断了
console.error("netrecv " + window.btoa(str));
document.getElementById("test_serial2").style.backgroundColor = "yellow";
pc.serial2.send_chars(str);
pc.serial2.send_chars(str);//add by hao,这个对应设备 /dev/ttyS1

if (0)//hao show network tcpdump
if (0)//add by hao ,show network tcpdump 这个地方打开性能会变好,但是没有网络协议的调试内容了
{
var data = str.slice(2);
//console.log("receiving " + data);
Expand Down Expand Up @@ -69,7 +63,7 @@ function hexy(s) {
return pad(s.toString(16), 2, '0')
}

function tcpdump_uint8array(data) {
function tcpdump_uint8array(data) { //add by hao,打印网络协议,目前支持的协议不多
try {
console.log("dest: " +
hexy(data[0]) + ":" +
Expand Down Expand Up @@ -152,6 +146,11 @@ function start(kernel_name)

params.serial2_write = function(data)
{
/*
add by hao,
这个地方是把jslinux的 /dev/ttyS1设备传来的数据转给websocket,和server端的tap设备进行通信
这里是核心交互代码
*/
//console.log("--->serial2_write");
/*
document.getElementById("test_serial2").innerHTML = character;
Expand Down Expand Up @@ -193,17 +192,19 @@ function start(kernel_name)

/* IDE drive. The raw disk image is split into files of
* 'block_size' KB.
* add by hao:
* 硬盘的加载部分,把大硬盘拆分成小块,加载时可以有限加载部分代码,降低网络延迟
* todo1:这部分后续计划改造成indexedDB ,来加快第二次访问的速度
* todo2:计划改造成browserfs的方式做文件系统
*/

//params.hda = { url: "hda%d.bin", block_size: 64, nb_blocks: 912 };
//params.hda = { url: "../jslinux-network/hda%d.bin", block_size: 64, nb_blocks: 912 };
params.hda = { url: "hao/hda%d.bin", block_size: 64, nb_blocks: 912 };
//params.hda = { url: "hao1/hda%d.bin", block_size: 64, nb_blocks: 912 };
//params.hdb = { url: "hdb%d.bin", block_size: 64, nb_blocks: 912 };

pc = new PCEmulator(params);
pc = new PCEmulator(params);//add by hao ,新建模拟器, 代码从这里开始读
// console.log("start 1");
init_state.params = params;

//add by hao: load the kernel
if (!kernel_name)
kernel_name = "vmlinux-2.6.20.bin";
//kernel_name = "vmlinux28.bin";
Expand All @@ -213,6 +214,7 @@ function start(kernel_name)

function start2(ret)
{
//add by hao ,加载初始文件,这个文件会从0x10000位置开始加载内核,在编译内核的时候指定的
if (ret < 0)
return;
init_state.start_addr = 0x10000;
Expand Down
9 changes: 3 additions & 6 deletions term.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/*
Javascript Terminal
Copyright (c) 2011 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's
permission.
add by hao:
终端的模拟器
这部分有很多开源项目可以替代
*/
"use strict";
if (!Function.prototype.bind) {
Expand Down
8 changes: 2 additions & 6 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/*
Javascript Utilities
Copyright (c) 2011-2012 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's
permission.
add by hao:
一些同步异步的工具类,这些是没有es6的情况下的一些hack,目前用promise都能更优雅的解决
*/
"use strict";

Expand Down

0 comments on commit f8abdc7

Please sign in to comment.