shouldn't this be illegal?


Here is the game board for ease:
The rule is that the squares which the king must pass through to castle cannot be under attack. Here, you rightfully point out that you are attacking his rook, but his king does not move through that square in order to castle. Therefore, castling is legal.

Taken from the site's page on castling:
- Your king can not have moved! Once your king moves, you can no longer castle, even if you move the king back to the starting square. Many strategies involve forcing the opponent’s king to move just for this reason!
- Your rook can not have moved! If you move your rook, you can’t castle on that side anymore! Both the king and the rook you are castling with can’t have moved!
- Your king can NOT be in check! Though castling often looks like an appealing escape, you can’t castle while you are in check! Once you are out of check, then you can castle! Unlike moving, being checked does not remove the ability to castle later!
- Your king can not pass through check! If the square the king moves over, or moves to would put you in check, you can’t castle! You’ll have to get rid of that pesky attacking piece first!

Nice win. Castling was legal there because it is only if the squares which the king passes through are attacked, not the rook.