Skip to content

Commit

Permalink
Downloads via proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
nihirash committed Dec 20, 2020
1 parent 89baa64 commit 0f39cf7
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 26 deletions.
43 changes: 43 additions & 0 deletions drivers/proxy.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
IFDEF PROXY
MODULE Wifi
; Same singature as wifi.openTCP
; HL - host pointer in gopher row
; DE - port pointer in gopher row
openTCP:
push de
push hl
EspCmdOkErr "AT+CIPCLOSE"
//EspCmdOkErr 'AT+CIPSTART="TCP","192.168.0.208",6912' // Replace here for yourown proxy. If you wish
EspCmdOkErr 'AT+CIPSTART="TCP","138.68.76.243",6912' // Replace here for yourown proxy. If you wish
jr c, .error
pop hl : ld de, hostBuff
.copyHost
ld a, (hl) : cp 9 : jr z, 1F : and a : jr z, 1F
ld (de), a : inc hl, de
jr .copyHost
1 xor a : ld (de), a
pop hl : ld de, portBuff
.copyPort
ld a, (hl) : cp 9 : jr z, 1F : and a : jr z, 1F
ld (de), a : inc hl, de
jr .copyPort
1 ld hl, hostBuff : call tcpSendZ
ld hl, portBuff : call tcpSendZ
xor a : ld (closed), a
ret
.error
pop hl : pop de
ret

continue:
EspCmdOkErr "AT+CIPSEND=1"
ret c
.wait
call Uart.read : cp '>' : jr nz, .wait
ld a, 'c' : call Uart.write
jp checkOkErr

hostBuff ds 96
portBuff ds 6
ENDMODULE
ENDIF
12 changes: 10 additions & 2 deletions drivers/wifi.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ closed db 1
; Initialize Wifi chip to work
init:
call Uart.init
IFNDEF EMU
EspCmdOkErr "ATE0"
jr c, .initError

EspCmdOkErr "AT+CIPSERVER=0"
EspCmdOkErr "AT+CIPCLOSE" ; Close if there some connection was. Don't care about result
EspCmdOkErr "AT+CIPMUX=0" ; Single connection mode
jr c, .initError
EspCmdOkErr "AT+CIPDINFO=0" ; Disable additional info
jr c, .initError
ENDIF
or a
ret
.initError
ld hl, .errMsg : call DialogBox.msgBox
scf
ret
.errMsg db "WiFi chip init failed!",0

IFNDEF PROXY
; HL - host pointer in gopher row
; DE - port pointer in gopher row
openTCP:
Expand All @@ -38,7 +41,12 @@ openTCP:
ld a, 10 : call Uart.write
xor a : ld (closed), a
jp checkOkErr

continue:
ret
ENDIF


checkOkErr:
call Uart.read
cp 'O' : jr z, .okStart ; OK
Expand Down
46 changes: 46 additions & 0 deletions engine/downloader.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
MODULE Downloader
; HL - gopher row
go:
ld hl, .msg : call DialogBox.msgNoWait
ld de, historyBlock.locator
ld hl, de
.findFileName
ld a, (de) : inc de
cp '/' : jr nz, .skip
ld hl, de
.skip
and a : jr nz, .findFileName
.copy
;; HL - filename pointer
ld de, filename
.copyFileName
ld a, (hl) : and a : jr z, 1F

ld (de), a : inc hl, de
jr .copyFileName

1 ld (de), a

call Fetcher.sendGopherRequest

ld hl, filename, B, Dos.FMODE_CREATE
call Dos.fopen : ld (fp), a

; Downloading started
.loadPackets
ld hl, buffer, (Wifi.buffer_pointer), hl
call Wifi.getPacket
ld a, (Wifi.closed) : and a : jr nz, .closedCallback
ld bc, (Wifi.bytes_avail), a, (fp), hl, buffer
call Dos.fwrite
call Wifi.continue
jr .loadPackets
.closedCallback
ld a, (fp) : call Dos.fclose
jp History.back

.msg db "Downloading file! Wait a bit - eveything will happens now!",0
fp db 0
filename ds 64
ENDMODULE
50 changes: 29 additions & 21 deletions engine/fetcher.asm
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
MODULE Fetcher

; HL - pointer to gopher page line
fetchFromNet:
call sendGopherRequest
ld hl, buffer, (Wifi.buffer_pointer), hl
.loadPackets
call Wifi.getPacket
ld a, (Wifi.closed) : and a : jr nz, .closedCallback
IFDEF PROXY
call Wifi.continue
ENDIF
jr .loadPackets
.closedCallback
jp MediaProcessor.processResource

error:
ld hl, .err : call DialogBox.msgBox
jp History.back
ret
.err db "Document fetch error! Check your connection or hostname!", 0


fetchFromFS:
xor a : ld hl, buffer, de, buffer + 1, bc, #ffff - buffer - 1, (hl), a : ldir
call UrlEncoder.extractPath
loadFile
ld hl, nameBuffer : call Dos.loadBuffer
jp MediaProcessor.processResource

sendGopherRequest:
ld hl, historyBlock.locator, de, requestBuffer
.copyRequest
ld a, (hl)
Expand All @@ -14,7 +41,7 @@ fetchFromNet:
.getDomainAndHost
ld hl, historyBlock.host, de, historyBlock.port
call Wifi.openTCP
jr c, .error
jr c, error
ld a, (historyBlock.mediaType) : cp Font.INPUT : jr nz, .performRequest

ld a, 0, bc, #ff, hl, requestBuffer : cpir : dec hl
Expand All @@ -27,26 +54,7 @@ fetchFromNet:
.performRequest
ld hl, requestBuffer
call Wifi.tcpSendZ
ld hl, buffer, (Wifi.buffer_pointer), hl
.loadPackets
call Wifi.getPacket
ld a, (Wifi.closed) : and a : jr nz, .closedCallback
jr .loadPackets
.error
ld hl, .err : call DialogBox.msgBox
jp History.back
ret
.closedCallback
jp MediaProcessor.processResource
.err db "Document fetch error! Check your connection or hostname!", 0

fetchFromFS:
xor a : ld hl, buffer, de, buffer + 1, bc, #ffff - buffer - 1, (hl), a : ldir
call UrlEncoder.extractPath
loadFile
ld hl, nameBuffer : call Dos.loadBuffer
jp MediaProcessor.processResource

requestBuffer ds #ff
ENDMODULE
1 change: 1 addition & 0 deletions engine/history/controller.asm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ navigate:
ld a, (depth) : cp total : jr nc, 1f
inc a : ld (depth), a
1
ld a,(historyBlock.mediaType) : cp Font.DOWNLOAD : jp z, Downloader.go
jp load

ENDMODULE
1 change: 1 addition & 0 deletions engine/resident-parts.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ nameBuffer ds #ff, 0
db 0
hostName ds 64
include "downloader.asm"
7 changes: 4 additions & 3 deletions main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
include "drivers/font.asm"
include "drivers/next.asm"
;DEFINE WIFI_DEBUG
;DEFINE EMU

; DEFINE WIFI_DEBUG
; DEFINE EMU
DEFINE PROXY
SLOT0_PAGE = 16
SLOT1_PAGE = 17
;; 0x0000 - 0x2000
Expand All @@ -25,6 +25,7 @@ SLOT1_PAGE = 17
include "screen-viewer/index.asm"
include "drivers/uart.asm"
include "drivers/wifi.asm"
include "drivers/proxy.asm"
; Render
include "render/index.asm"
; History data placed here
Expand Down
3 changes: 3 additions & 0 deletions render/gopher-page.asm
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ navigate:
cp '7' : jp z, .input
jp workLoop
.load
push hl
call getIcon
pop hl
jp History.navigate
.input
push hl
Expand Down

0 comments on commit 0f39cf7

Please sign in to comment.